gpt4 book ai didi

java - 调整 JButton 大小会导致其消失

转载 作者:行者123 更新时间:2023-11-30 07:59:15 26 4
gpt4 key购买 nike

我希望所有按钮的大小相同。带有“打开保存”文本的按钮是最宽的,因此我想使用该按钮作为调整其他按钮大小的指标。但是,当我在程序中有这行代码时,按钮不会显示: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 resizing with resizing

最佳答案

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,它已经善意地应用了该大小,现在您的按钮不再可见。

现在,如果我对您想要执行的操作的推测正确,请尝试将 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

关于java - 调整 JButton 大小会导致其消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32194177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com