【问题标题】:How to close a Frame by clickin on the top right "x"?如何通过单击右上角的“x”来关闭框架?
【发布时间】:2012-05-09 18:33:19
【问题描述】:

在 Java 中得到一个框架:

 Frame AFrame = new Frame("Frame with components"); 

默认情况下,框架右上角的“x”关闭按钮不起作用。 我该如何设置?

【问题讨论】:

  • 您确实应该使用 Swing 组件,而不是 AWT 组件。话虽如此,请将Frame 替换为JFrame

标签: java awt frame


【解决方案1】:

你应该使用JFrame,然后

JFrame AFrame = new JFrame("Frame with components");
AFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

但如果你坚持使用 Frame 则添加一个监听器:

AFrame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});

【讨论】:

猜你喜欢
  • 2014-04-07
  • 2012-04-27
  • 1970-01-01
  • 2012-01-13
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 2019-12-21
相关资源
最近更新 更多