为什么GridBagLayout将我的组件放在中心而不是放在角落?

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

 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的post:

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

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

编辑:

来自Oracle的GridBagLayout指南 :

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

您需要在Swing教程中进一步阅读有关weightX/weightY的部分,其中指出:

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

您指定了weightX但不是weightY。

编辑,这比我想象的要复杂得多。 看来你还需要指定:

 constraints.anchor = GridBagConstraints.FIRST_LINE_START; 

两个组件的重量相加。

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

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

编辑:好吧忘了诀窍:(正确的方式是作为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); } } 

这对我有用:

 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; }