呼吁窗口功能关闭 [英] Calling function on windows close

查看:160
本文介绍了呼吁窗口功能关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java:我有使用NetBeans GUI Builder中的GUI构建

在GUI类被延长一个JFrame创建

公共类ArduinoGUI扩展javax.swing.JFrame中

和GUI中显示使用:

 了java.awt.EventQueue.invokeLater(新的Runnable(){
    公共无效的run(){
        新ArduinoGUI()调用setVisible(真);
    }
}

所以我没有在其上调用帧中的实际帧的对象。,所以在这种情况下,我怎么可以覆盖的windowClosed 函数,因为我有打电话给一个特定的功能整理应用程序退出之前的串行连接。

编辑:这里是code明确如下回答:

  @覆盖
公共无效processWindowEvent(WindowEvent五){
    如果(e.getID()== WindowEvent.WINDOW_CLOSING){
        arduino.close();
        的System.out.println(Arduino的关闭());
        处置();
    }


解决方案

创建类processWindowEvent的方法(这是JFrame的子类),如果你还没有这样做。该方法采用WindowEvent对象作为参数。这个方法里面添加一个if块这样的:

 如果(e.getID()== WindowEvent.WINDOW_CLOSING){    //...Do你只需要闭幕前做什么}

e是WindowEvent对象传递参数的方法。

Using Java: I have a GUI built using the netbeans GUI builder.

The GUI class was created by extending a jFrame

public class ArduinoGUI extends javax.swing.JFrame

and the GUI displayed using:

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {                    
        new ArduinoGUI().setVisible(true);                    
    }
}

Therefore I don't have an actual frame object on which to call frame., so how in this case can I override the windowClosed function, because I have to call a specific function to tidy up a serial connection before the app exits.

Edit: here is the code explicit as answered below:

@Override
public void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
        arduino.close();
        System.out.println("Arduino Close()");
        dispose();
    }

解决方案

Create "processWindowEvent" method in your class (which is subclass of JFRame) if you haven't already done. That method takes WindowEvent object as parameter. inside that method add an if block like this :

if(e.getID() == WindowEvent.WINDOW_CLOSING){

    //...Do what you need to do just before closing

}

e is the WindowEvent object passed parameter to method.

这篇关于呼吁窗口功能关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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