2
我有一個使用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);
對容納組件列的容器使用GridLayout。用'new GridLayout(0,1,0,vGap)'初始化它,它代表1列,可變的行數。 vGap參數必須是表示組件之間垂直間隙的整數。 –