当我按下JButton时如何关闭GUI? [英] How to close a GUI when I push a JButton?
问题描述
有人知道如何使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屋!