我有一个使用FlowLayout的JPanel和一个垂直排列组件的Box。 我想要的是将其他组件的宽度大小设置为“删除列”按钮。 我试着线Java Swing - Button不改变宽度的大小
removeColumnButton.setPreferredSize(new Dimension(130, 25));
改变大小,但我只能改变其高度,宽度没有大小。
下面是面板和代码的屏幕截图:
JPanel eastPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
Box eastPanelBox = Box.createVerticalBox();
addNewColumnButton = new JButton("Add New Column");
addNewColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(addNewColumnButton);
eastPanelBox.add(Box.createVerticalStrut(5));
removeColumnButton = new JButton("Remove Column");
removeColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
removeColumnButton.setPreferredSize(new Dimension(130, 25));
eastPanelBox.add(removeColumnButton);
eastPanelBox.add(Box.createVerticalStrut(5));
columnField = new JTextField();
columnField.setAlignmentX(Box.CENTER_ALIGNMENT);
columnField.setPreferredSize(new Dimension(130, 25));
eastPanelBox.add(columnField);
eastPanelBox.add(Box.createVerticalStrut(5));
columnListCB = new JComboBox(cBoxModel);
columnListCB.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(columnListCB);
eastPanelBox.add(Box.createVerticalStrut(5));
calculateColumnButton = new JButton("Calculate Column");
calculateColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(calculateColumnButton);
eastPanel.add(eastPanelBox);
2014-05-05
Xerath
+1
对容纳组件列的容器使用GridLayout。用'new GridLayout(0,1,0,vGap)'初始化它,它代表1列,可变的行数。 vGap参数必须是表示组件之间垂直间隙的整数。 –