gpt4 book ai didi

java - 为什么我的 Button 不总是在 GUI 上弹出?

转载 作者:行者123 更新时间:2023-12-04 05:48:33 25 4
gpt4 key购买 nike

所以我在做一个游戏,我想在窗口中弹出一个jbutton,当你点击它时你可以登录。问题是当我开始游戏时,它并不总是弹出,这有点烦人。唯一的想法是问题是我做事的顺序。这是代码:

public static void createWindow() {
ImagePanel panel = new ImagePanel(
new ImageIcon(backgroundFile).getImage()); //used for the background


JButton login = new JButton(new AbstractAction("Login") {
public void actionPerformed(ActionEvent e) {
Login.createWindow();
}
});
login.setBounds(300, 300, 100, 100);


frame.getContentPane().add(panel); //sets the background to a pic
frame.setJMenuBar(MenuBar.menuBarCreator()); creates the menu bar
frame.pack();
frame.setResizable(false);
frame.setTitle("*Game Title* Beta 0.0.1 ADMINISTRATOR VERSION");
frame.setSize(ImagePanel.img.getWidth(null),
ImagePanel.img.getHeight(null));
frame.setLocation(Monitor.setLocationHeight(),
Monitor.setLocationWidth());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
StreamingLineSound.start(soundFile); //starts a music file
frame.add(login);

}

任何帮助都会很棒。所以基本上我想要的只是一个想法,为什么它不会一直弹出。谢谢

最佳答案

代码行 frame.setVisible(true);必须是 public static void createWindow() { 中的最后一行, 因为你显示 JFrame然后添加 frame.add(login);

关于java - 为什么我的 Button 不总是在 GUI 上弹出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375634/

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