gpt4 book ai didi

java - 当程序应该使主页恢复可见时,为什么它会终止程序?

转载 作者:行者123 更新时间:2023-12-02 04:03:57 26 4
gpt4 key购买 nike

按钮 Action 监听器。 (r 是我的类的调用方法,我在上面调用它 Run r= new Run();。它将窗口设置为不可见,但当它应该使其恢复可见时,程序会关闭而不会出现任何错误。尝试代替 setVisible(false); dispose(); 但同样的问题。

about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame a=new JFrame("About");

a.addWindowListener(new WindowListener(){
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowClosed(WindowEvent arg0) {


}

@Override
public void windowClosing(WindowEvent arg0) {
r.gui.setVisible(true);

}

@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowOpened(WindowEvent arg0) {
r.gui.setVisible(false);

}
});

a.setSize(400, 400);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setVisible(true);
a.setLayout(null);

JLabel lbl=new JLabel("This game was made by your lovely neighbourhood takisp22");
lbl.setSize(500,50);
lbl.setLocation(0,0);
a.setLocation(100,50);
a.add(lbl);
}
});

运行该程序的其他类:

    import javax.swing.JFrame;

public class Run {
public static GameAim gui=new GameAim();
public static void main(String[] args){

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Aim Training");
gui.setSize(1280, 800);
gui.setVisible(true);
gui.setLocation(100,50);
gui.setResizable(false);

gui.openFile();
gui.readFile();
gui.closeFile();
}
}

最佳答案

因为这个:

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

当您关闭 gui JFrame 变量时,程序退出。

不要使用此选项,而应使用 JFrame.DISPOSE_ON_CLOSE。说到这里,请阅读:The Use of Multiple JFrames, Good/Bad Practice? 。您的用户不会喜欢向他们推多个窗口。使用 CardLayout 交换 View -- tutorial link .

我们还需要在某个时候讨论使用 a.setLayout(null); 的弊端。
;)

关于java - 当程序应该使主页恢复可见时,为什么它会终止程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34567362/

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