【问题标题】:BorderLayout center command won't centerBorderLayout 中心命令不会居中
【发布时间】:2011-11-26 05:39:46
【问题描述】:

我正在尝试将两个 JButtons 并排放置在 JFrame 的中心,当 JFrame 重新调整大小时,按钮不会重新调整大小。

为此,我将两个按钮放置在带有FlowLayout 的面板中,然后将其放置在带有中心BorderLayout 的面板中。

但是,以下代码不会在BorderLayout 的中心显示所选面板。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class test extends JFrame {

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JButton button1 = new JButton("one");
    private JButton button2 = new JButton("two");

    public test() {
        panel1.setLayout(new FlowLayout());
        panel1.add(button1);
        panel1.add(button2);

        panel2.setLayout(new BorderLayout());
        panel2.add(panel1, BorderLayout.CENTER);

        this.add(panel2);
    }

    public static void main(String[] args) {
        test frame = new test();
        frame.setVisible(true);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

【问题讨论】:

  • 请学习java命名约定并遵守它们

标签: java swing layout


【解决方案1】:

GridBagLayout 设置为panel1

 panel1.setLayout(new GridBagLayout());

编辑:

@trashgod:我必须弄清楚默认约束是如何做到的。

因为字段GridBagLayout.defaultConstraints

此字段包含一个网格包约束实例,其中包含默认值 值,因此如果组件没有关联的网格包约束 有了它,那么组件将被分配一个副本 默认约束。

在正常实践中,必须创建GridBagConstraints 对象并设置字段以在每个对象上指定约束

引用tutorial

设置约束的首选方法 组件是使用 Container.add 变体,传递给它一个 GridBagConstraints 对象

【讨论】:

  • +1 有效!现在,我必须弄清楚默认约束是如何做到的。 :-)
  • @trashgod,来自 Swing 教程中关于使用 weightx/weighty 约束:Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container...
【解决方案2】:

看似不直观,但行为正确。

panel1 分配了尽可能多的可用屏幕空间,因为它是panel2 内的唯一组件。 FlowLayout 然后从可用空间的顶部开始布局组件,并且只有在填充了所有可用的水平空间后才将组件进一步向下放置。因此,您的两个按钮位于框架的顶部。

您可以尝试改用Box

public class test extends JFrame {

    private JComponent box = Box.createHorizontalBox();
    private JButton button1 = new JButton("one"); 
    private JButton button2 = new JButton("two"); 

    public test() {
        box.add(Box.createHorizontalGlue());
        box.add(button1);
        box.add(button2);
        box.add(Box.createHorizontalGlue());
        this.add(box);
    }

    ...
}

一个水平框自动将组件垂直居中,两个粘合组件占用任何额外的可用水平空间,以便按钮位于框的中心。

【讨论】:

    【解决方案3】:

    JPanel 默认使用 FlowLayout,FlowLayout 默认使用居中对齐...这对我来说似乎比使用 GridBagLayout 甚至 BoxLayout 更容易。如果您不希望按钮在面板变得太小时“换行”,您可以设置一个最小尺寸。

    package example;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class FlowLO extends JFrame
    {
        public static void main(String[] args)
        {
            FlowLO flowLO = new FlowLO();
            flowLO.go();
        }
        public void go()
        {
            JPanel centeredPanel = new JPanel();
            centeredPanel.add(new JButton("one"));
            centeredPanel.add(new JButton("two"));
            getContentPane().add(centeredPanel);
            pack();
            setVisible(true);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多