简体   繁体   English

如何使用JButton ActionListener关闭程序?

[英]How do I shutdown my program with a JButton ActionListener?

I have a button, and a buttonhandler(ActionEvent) for it. 我有一个按钮,以及一个buttonhandler(ActionEvent)。 Now, I want to make it so that, when you click the button, your program shuts down. 现在,我想这样做,以便在单击按钮时关闭程序。 How would I go about doing this? 我将如何去做呢? My buttonhandler code: 我的按钮处理程序代码:

class ButtonHandler implements ActionListener{
    public void actionPerformed( ActionEvent e){

    }
}

So I basicly need to shutdown the whole JFrame. 因此,我基本上需要关闭整个JFrame。

Your ButtonHandler would have a reference to the JFrame it's a member of and call JFrame.dispose(); 您的ButtonHandler将引用它的成员JFrame并调用JFrame.dispose();。

class ButtonHandler implements ActionListener{
    final JFrame parent; 
    public ButtonHandler(JFrame p) { parent = p; }

    public void actionPerformed( ActionEvent e){
        parent.dispose();
    }
}

如果要关闭整个程序,则可以使用System.exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ActionListener添加到扩展JButton的类的实例中? - How do I add an ActionListener to an instance of my class that extends JButton? 如何使用 actionListener 更改 JButton 的颜色 - How do I change the color of a JButton with actionListener 如何将ActionListener放在使用Jbuttons的ArrayList创建的JButton上 - How do I put an ActionListener on a JButton created with an ArrayList of Jbuttons 如何在JButton ActionListener中访问变量? - How can I access a variable in an JButton ActionListener? 如何将 ActionListener 添加到 JButton - How to add ActionListener to JButton 如何使ActionListener按钮在我的Java GUI程序中保持骰子? - how do i make an ActionListener button to hold a dice in my java GUI program? 如何使带有匿名内部类actionlistener的JButton在单击时自行删除? - How do I make a JButton with an anonymous innerclass actionlistener remove itself on click? Java:如何将变量从JButton ActionListener传递给主类? - Java: How do I pass variables from JButton ActionListener to main class? 如何从方法中测试JButton ActionListener? (单元测试) - How do I test JButton ActionListener from within a method? (unit testing) 如何在代码中激活 JButton ActionListener(单元测试目的)? - How do I activate JButton ActionListener inside code (unit testing purposes)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM