【发布时间】:2015-06-20 01:51:04
【问题描述】:
我一直在搞乱 GridBagLayout 并且遇到了一些麻烦。首先,我试图让标签从我完成的左上角开始:
JPanel right = new JPanel(new GridBagLayout());
right.setPreferredSize(new Dimension(333,600));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
JLabel testLabel = new JLabel();
testLabel.setText("Test");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.weightx = 1;
right.add(testLabel, gbc);
现在我想在这个标签下直接添加另一个标签:
JLabel test2Label = new JLabel();
test2Label.setText("test2");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weighty = 1;
gbc.weightx = 1;
right.add(test2Label, gbc);
而不是将它直接放在第一个下面,而是将它放在面板的中间。这是因为我认为的重量和重量x,但如果我改变这些,那么它不会将标签放在左上角。我怎样才能让它工作?抱歉,如果不清楚,英语不是我最好的语言,所以如果您需要我澄清,请告诉我。 -谢谢
【问题讨论】:
标签: java