如何使GridBagLayout
中的组件占据相同的宽度?我已经尝试过GridBagConstraint.weightx
,但是它不起作用。
static void test3() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(480, 360);
Container pane = f.getContentPane();
pane.setLayout(new GridBagLayout());
String[] a = {
"Lorem ipsum dolor sit amet",
"consectetur adipisicing",
"elit, ",
"sed do",
"eiusmod",
};
for (int i = 0; i < 5; i++) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = i % 3;
c.gridy = i / 3;
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
f.add(new JButton(a[i]), c);
}
f.setVisible(true);
}
最佳答案
static void test3() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 360);
Container pane = f.getContentPane();
pane.setLayout(new GridBagLayout());
String[] a = {
"Lorem ipsum dolor sit amet",
"consectetur adipisicing",
"elit,",
"sed do",
"eiusmod",
};
for (int i = 0; i < 5; i++) {
JButton test=new JButton(a[i]);
GridBagConstraints c = new GridBagConstraints();
c.gridx = i %3;
c.gridy = i /3;
c.fill = GridBagConstraints.HORIZONTAL;
test.setPreferredSize(new Dimension(250,25));
f.add(test, c);
}
f.setVisible(true);
}
您需要为按钮设置首选大小。这会给你想要的结果