为什么GridBagLayout集中我的组件而不是把它放在角落?

时间:2011-09-01 19:05:40

标签: java swing user-interface gridbaglayout

到目前为止,我设法尽可能避免使用GridBagLayout(手动代码),但这次我无法避免,我正在阅读SUN的教程GridBagLayout 到目前为止它还不顺利。我想我很想念一些东西 例如,我尝试以下代码(类似于SUN的帖子中的代码):

public class MainFrame extends JFrame { 


    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame
     */
    public MainFrame() {
        super();
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container mainContainer = getContentPane();

        mainContainer.setLayout(new GridBagLayout());       

        //add label
        JLabel someLabel = new JLabel("Label 1:");
        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        //constraints.anchor = GridBagConstraints.FIRST_LINE_START;
        //constraints.weightx = 0.5;
        mainContainer.add(someLabel, constraints);      

        JTextField someText = new JTextField(30);

        constraints = new GridBagConstraints();

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.weightx = 0.5;
        mainContainer.add(someText, constraints);

        //
    }

}

我在框架的中心中将标签和文本字段放在另一个旁边。
但我期待它们会出现在左上角,因为gridx和gridy为标签为0 即使我设置constraints.anchor = GridBagConstraints.FIRST_LINE_START;仍然是相同的结果 我错了吗? 来自SUN的帖子:

  

指定组件左上角的行和列。该   最左边的列有地址gridx = 0,顶行有地址   gridy = 0。

5 个答案:

答案 0 :(得分:13)

constraints.weighty = 1;添加到JLabel约束,将constraints.anchor = GridBagConstraints.NORTHWEST;添加到TextField约束。

编辑:

来自Oracle的GridBagLayout guide

  

较大的数字表示组件的行或列应该获得更多空间。对于每列,权重与为该列中的组件指定的最高权重x相关,每个多列组件的权重在组件所在的列之间以某种方式分割。类似地,每行的权重与为a指定的最高权重相关。该行中的组件。额外的空间倾向于最右边的列和底行。

答案 1 :(得分:13)

您需要在Swing教程中进一步了解weightX/weightY部分,其中指出:

除非您为weightx或weighty指定至少一个非零值,否则所有组件都会在其容器的中心聚集在一起。

您指定了weightX但不是weightY。

编辑,这比我想象的要复杂得多。您似乎还需要指定:

constraints.anchor = GridBagConstraints.FIRST_LINE_START;

两个组件的重量相加。

答案 2 :(得分:2)

您可以通过使用技巧,在行后添加虚拟组件并展开它以填充垂直空间来实现此目的。您也可以重复使用约束,无需创建新对象:

编辑:好吧忘了诀窍:(正确的方法是Deon Botha和BenCole说,我已经使用锚点更新了我的代码

不要接受此答案,接受Deon's或Ben's

public class MainFrame extends JFrame { 
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

public MainFrame() {
    super();
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container mainContainer = getContentPane();
    mainContainer.setLayout(new GridBagLayout());       

    JLabel someLabel = new JLabel("Label 1:");
    JTextField someText = new JTextField(30);

    GridBagConstraints constraints = new GridBagConstraints();
    constraints.anchor = GridBagConstraints.FIRST_LINE_START;

    constraints.gridx = 0;
    constraints.gridy = 0;    
    constraints.weightx = 1.0;
    mainContainer.add(someLabel, constraints);      

    constraints.gridx = 1;                       
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;        
    mainContainer.add(someText, constraints);                       
}
}

答案 3 :(得分:2)

我可能不会直接回答您的问题,但请相信我您应该使用IDE对布局进行试验和错误。我个人建议Netbeans。在那里你可以拖放,然后看看属性。首先,您将在属性检查器中给出一些默认值,因此自动生成的代码较少。但是,一旦你开始使用布局,你就可以看到代码,并且很清楚地了解你的工作方式。

答案 4 :(得分:1)

这对我有用:

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel2 = new javax.swing.JPanel();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jComboBox5 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 204, 51));
        setMinimumSize(new java.awt.Dimension(800, 600));
        getContentPane().setLayout(null);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(jComboBox3, gridBagConstraints);

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(jComboBox4, gridBagConstraints);

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel2.add(jComboBox5, gridBagConstraints);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(30, 40, 150, 260);

        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JPanel jPanel2;
}