Java 如何通过单击右上角的“输入”关闭框架;x";?
在Java中获得了一个框架:Java 如何通过单击右上角的“输入”关闭框架;x";?,java,awt,frame,Java,Awt,Frame,在Java中获得了一个框架: Frame AFrame = new Frame("Frame with components"); 默认情况下,框架右上角的“x”关闭按钮不工作。 我如何设置呢?您应该使用 JFrame AFrame = new JFrame("Frame with components"); AFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 但如果您坚持使用框架,请添加一个侦听器: AFrame.addWind
Frame AFrame = new Frame("Frame with components");
默认情况下,框架右上角的“x”关闭按钮不工作。
我如何设置呢?您应该使用
JFrame AFrame = new JFrame("Frame with components");
AFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
但如果您坚持使用框架,请添加一个侦听器:
AFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
您确实应该使用Swing组件,而不是AWT组件。也就是说,将
Frame
替换为JFrame
。同意,OP可能应该使用JFrame
,仅供参考,以下是如何关闭AWT帧:系统。退出(0)
可能有点过火:可能是AFrame。这个.dispose()
是更好的选择。看:@BartKiers谢谢,我不知道。你每天都会学到新东西。rame.setDefaultCloseOperation(JFrame.dispse\u ON\u CLOSE)代码>