2017-05-29 67 views
0

我正在使用GB布局表单。目前有两行,每行包含6个元素(第一行包含特征标签,第二个文本区域)。我的问题是应该在X 5 Y 1中显示的元素显示在X 6 Y 0中。请您指出我所犯的错误吗?Java Gridbag missalign - 第一行显示第二行的最后一个单元格

public GUI() { 
    super("Bla"); 
    GridBagLayout layout = new GridBagLayout(); 
    setLayout(layout); 
    GridBagConstraints gbc = new GridBagConstraints();  
    [enter image description here][1]gbc.insets = new Insets(10,10,10,10); 
    Container con = getContentPane(); 

    panel1 = new JPanel(); 

    labBrawn = new JLabel("Brawn"); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    con.add(labBrawn, gbc); 

    labAgility = new JLabel("Agility"); 
    gbc.gridx = 1; 
    gbc.gridy = 0; 
    con.add(labAgility, gbc); 

    labIntellect = new JLabel("Intellect"); 
    gbc.gridx = 2; 
    gbc.gridy = 0; 
    con.add(labIntellect, gbc); 

    labCunning = new JLabel("Cunning"); 
    gbc.gridx = 3; 
    gbc.gridy = 0; 
    con.add(labCunning, gbc); 

    labWillpower = new JLabel("Willpower"); 
    gbc.gridx = 4; 
    gbc.gridy = 0; 
    con.add(labWillpower, gbc); 

    labPresence = new JLabel("Presence"); 
    gbc.gridx = 5; 
    gbc.gridy = 0; 
    con.add(labPresence, gbc); 



    txtBrawn = new JTextField("", 2); 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    con.add(txtBrawn, gbc); 

    txtAgility = new JTextField("", 2); 
    gbc.gridx = 1; 
    gbc.gridy = 1; 
    con.add(txtAgility, gbc); 

    txtIntellect = new JTextField("", 2); 
    gbc.gridx = 2; 
    gbc.gridy = 1; 
    con.add(txtIntellect, gbc); 

    txtCunning = new JTextField("", 2); 
    gbc.gridx = 3; 
    gbc.gridy = 1; 
    con.add(txtCunning, gbc); 

    txtWillpower = new JTextField("", 2); 
    gbc.gridx = 4; 
    gbc.gridy = 1; 
    con.add(txtWillpower, gbc); 

    txtPresence = new JTextField("", 2); 
    gbc.gridx = 5; 
    gbc.gridy = 1; 
    con.add(txtPresence); 


} 

回答

1

使用GridBagLayout需要调用

附加(色差分量,对象约束)

添加你忘了GridBagConstraints对象con.add(txtPresence);您的最后一个组件。没有它,组件将被放在第一行的最后一个组件后面,因为它是第一个空闲空间,它不会受到约束(它只会使Y = 0,并进入下一个可用的X)。如果您将在JPanel中添加组件GridBagLayout而没有GridBagConstraints对象,它们将被放入一行中。

记得设置weights,fillanchor与您的GridBagConstraints对象。

您还可以在github上看到我的GridBagManager回购。它包含演示它如何工作的演示。您仍然应该了解GridBagLayout的工作原理。

SSCCE:(你的代码的quickfix)

public class GUI extends JFrame { 

    public static void main(String[] args) { 
     GUI gui = new GUI(); 
     gui.setSize(600, 600); 
     gui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     SwingUtilities.invokeLater(() -> { 
      gui.setVisible(true); 
     }); 
    } 

    public GUI() { 
     super(); 

     //those should be initialized outside 
     JLabel labBrawn = new JLabel("Brawn"); 
     JLabel labAgility = new JLabel("Agility"); 
     JLabel labIntellect = new JLabel("Intellect"); 
     JLabel labCunning = new JLabel("Cunning"); 
     JLabel labWillpower = new JLabel("Willpower"); 
     JLabel labPresence = new JLabel("Presence"); 

     JTextField txtBrawn = new JTextField("", 2); 
     JTextField txtAgility = new JTextField("", 2); 
     JTextField txtIntellect = new JTextField("", 2); 
     JTextField txtCunning = new JTextField("", 2); 
     JTextField txtWillpower = new JTextField("", 2); 
     JTextField txtPresence = new JTextField("", 2); 
     //-------------------------------------------- 

     //here goes actual GUI 
     GridBagLayout gridBagLayout = new GridBagLayout(); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.insets = new Insets(10, 10, 10, 10); 
     gbc.anchor = GridBagConstraints.NORTH; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 

     JPanel dataInputPanel = new JPanel(); 
     dataInputPanel.setLayout(gridBagLayout); 

     gbc.weightx = 1; 
     gbc.weighty = 0; 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     dataInputPanel.add(labBrawn, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     dataInputPanel.add(labAgility, gbc); 

     gbc.gridx = 2; 
     gbc.gridy = 0; 
     dataInputPanel.add(labIntellect, gbc); 

     gbc.gridx = 3; 
     gbc.gridy = 0; 
     dataInputPanel.add(labCunning, gbc); 

     gbc.gridx = 4; 
     gbc.gridy = 0; 
     dataInputPanel.add(labWillpower, gbc); 

     gbc.gridx = 5; 
     gbc.gridy = 0; 
     dataInputPanel.add(labPresence, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtBrawn, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtAgility, gbc); 

     gbc.gridx = 2; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtIntellect, gbc); 

     gbc.gridx = 3; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtCunning, gbc); 

     gbc.gridx = 4; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtWillpower, gbc); 

     gbc.gridx = 5; 
     gbc.gridy = 1; 
     gbc.weighty = 1; 
     dataInputPanel.add(txtPresence, gbc); 

     dataInputPanel.setBackground(Color.red); 
     this.add(dataInputPanel, BorderLayout.NORTH); 
    } 
} 
+0

哦,我...谢谢你,对不起,这样的问题困扰。 – Faire

+0

@Faire Yup,没问题。总是会发生在我身上。这就是为什么我创建GridBagManager并且我的抽象面板有自己的add()方法,它调用add(comp,gbc),GridBagConstraints gbc是这个类的一个字段。就那么简单。看看它,快乐的编码! – bigby

相关问题