如何在推送JButton时关闭GUI [英] How to close a GUI when i push a JButton

查看:160
本文介绍了如何在推送JButton时关闭GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何让jbutton关闭gui?我认为它就像 System.CLOSE(0); 但是没有用。它也可以是 exitActionPerformed(evt); ,但这也不起作用。只需要一行代码即可。

Does anyone know how to make a jbutton close a gui? I think it is like System.CLOSE(0); but that didnt work. it also could be exitActionPerformed(evt);, but that didn't work either. just the line of code will work.

编辑:别介意人。答案是 System.exit(0); 。感谢您的帮助!

never mind guys. the answer was System.exit(0);. thanks for the help though!

推荐答案

添加按钮:

JButton close = new JButton("Close");

添加一个ActionListener:

Add an ActionListener:

close.addActionListner(new CloseListener());

为实现ActionListener接口的Listener添加一个类并覆盖其主函数:

Add a class for the Listener implementing the ActionListener interface and override its main function:

private class CloseListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //DO SOMETHING
        System.exit(0);
    }
}

这可能不是最好的方式,但它是一个指向开始。例如,该类可以公开,而不是另一个类中的私有类。

This might be not the best way, but its a point to start. The class for example can be made public and not as a private class inside another one.

这篇关于如何在推送JButton时关闭GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆