我希望所有按钮的尺寸都相同。带有文本“打开保存”的那个是最宽的,所以我想使用那个作为调整其他按钮大小的度量。但是,当程序中包含以下代码行时,该按钮将不会显示:newButton.setPreferredSize(openSaveButton.getSize());

这是到目前为止的完整程序:

public class Project1 implements ActionListener {

    // The frames
    private JFrame mainMenu = new JFrame("Main Menu");
    private JPanel mainPanel = new JPanel();
    private JFrame game = new JFrame("Game");
    private JPanel gamePanel = new JPanel();
    // Controls
    private JButton newButton = new JButton("New");
    private JButton openSaveButton = new JButton("Open Save");
    private JButton exitButton = new JButton("Exit");
    private JTextArea textArea = new JTextArea();

    public static void main(String[] args) {
        new Project1();
    }

// Constructors
    public Project1() {

        mainPanel.setLayout(new GridBagLayout());
        addItem(mainPanel, newButton, 0, 0, 1, 1, GridBagConstraints.CENTER);
        newButton.addActionListener(this);
        newButton.setPreferredSize(openSaveButton.getSize());
        addItem(mainPanel, openSaveButton, 0, 1, 1, 1, GridBagConstraints.CENTER);
        openSaveButton.addActionListener(this);
        addItem(mainPanel, exitButton, 0, 2, 1, 1, GridBagConstraints.CENTER);
        exitButton.addActionListener(this);

        mainMenu.add(mainPanel, BorderLayout.NORTH);
        openFrame(mainMenu, 10, 10, JFrame.EXIT_ON_CLOSE);

        gamePanel.setLayout(new GridBagLayout());
        addItem(gamePanel, textArea, 0, 0, 1, 1, GridBagConstraints.CENTER);
        openFrame(game, 200, 10, JFrame.DO_NOTHING_ON_CLOSE);

    }

// Setters
// Getters
// ActionListener override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == newButton) {
            newGame();
        } else if (e.getSource() == openSaveButton) {
            openFile();
        } else if (e.getSource() == exitButton) {
            System.exit(0);
        }
    }

// Other functions and procedures
    private void openFrame(JFrame what, int x, int y, int operation) {

        what.pack();
        what.setLocation(x, y);
        what.setVisible(true);
        what.setResizable(false);
        what.setDefaultCloseOperation(operation);
    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height,
                    int align) {

        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(5, 5, 5, 5);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }

    private void newGame() {

    }

    private void openFile() {

    }

}


以下是发生的情况的两个屏幕截图,没有一行,带有:

java - 调整JButton的大小使其消失-LMLPHP java - 调整JButton的大小使其消失-LMLPHP

最佳答案

但是,当程序中包含以下代码行时,该按钮将不会显示:newButton.setPreferredSize(openSaveButton.getSize());


然后,不要这样做。

当您调用openSaveButton.getSize()时,openSaveButton没有大小,它是0x0,所以现在您告诉布局管理器newButton应该具有首选的大小0x0,这是我们很好地应用的,现在您按一下不再可见。

现在,如果我对您要执行的操作正确无误,请尝试在gc.fill = GridBagConstraints.NONE;方法中将gc.fill = GridBagConstraints.HORIZONTAL;更改为addItem

有关更多详细信息,请参见How to Use GridBagLayout

您可能还想看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?The Use of Multiple JFrames, Good/Bad Practice?Initial Threads

07-28 13:50