有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java调整JButton的大小会导致它消失

我希望我所有的纽扣大小都一样。文本为“Open Save”的按钮最宽,因此我想用它作为调整其他按钮大小的指标。但是,当程序中有这行代码时,按钮不会显示: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() {

    }

}

以下是两个屏幕截图,显示了在没有线条的情况下发生的情况,以及:

without resizingwith resizing


共 (1) 个答案

  1. # 1 楼答案

    However, when I have this line of code in the program, the button won't display: newButton.setPreferredSize (openSaveButton.getSize ());

    那么,不要这样做

    当你调用openSaveButton.getSize()时,openSaveButton没有大小,它是0x0,所以现在你告诉布局管理器newButton应该有一个首选大小0x0,它正在应用,现在你的按钮不再可见

    现在,如果我猜对了你想做的事情,试着在你的addItem方法中改变gc.fill = GridBagConstraints.NONE;gc.fill = GridBagConstraints.HORIZONTAL;

    有关更多详细信息,请参见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