简体   繁体   English

Java Swing-按钮不会更改宽度的大小

[英]Java Swing - Button doesn't change size of the width

I have a JPanel which uses the FlowLayout, and a Box which has components arranged vertically. 我有一个使用FlowLayout的JPanel和一个具有垂直排列组件的Box。 What I want, is to set the same width size of the other components to button "Remove Column". 我想要的是将其他组件的相同宽度大小设置为“删除列”按钮。 I've tried to change the size with line 我试图用线更改大小

removeColumnButton.setPreferredSize(new Dimension(130, 25));

but I can only change size of the height, not width. 但我只能更改高度的大小,而不能更改宽度。

Below is screenshot of the panel and code: 以下是面板和代码的屏幕截图:

在此处输入图片说明

    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);

Use a GridLayout for the container holding the column of components. 将GridLayout用于保存组件列的容器。 Initialize it with 用初始化

int vGap = 5;
new GridLayout(0, 1, 0, vGap)

which stands for 1 column, variable number of rows. 代表1列,行数可变。 The vGap parameter must be an int that represents the vertical gap between components. vGap参数必须是一个int,它表示组件之间的垂直间隙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM