如何在其他类中访问此关键字

时间:2015-09-06 08:03:44

标签: java swing windowlistener

尝试使用Java编程在不同的类中访问此关键字时遇到问题。我尝试了Context,class.this但还没有帮助......

我已经使用NetBeans gui builder创建了一个项目,我希望当我点击按钮时表单被处理掉......

主类包含用于处理JFrame表单的click事件 BestQSystems.java:

import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import javax.naming.Context;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Benson
 */
public class CloseWindow {
    public  static void closeWindow(){
        WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(widnowEvent);
    }
}

关闭JFrame的类:CloseWindow.java

WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);

此行中出现错误this请告知我如何在其他课程中访问{{1}}个关键字。

1 个答案:

答案 0 :(得分:2)

您可以将this的引用传递给其他方法。例如:

BestQSystems.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      CloseWindow.closeWindow(this); 
}

并在CloseWindow.java中

public class CloseWindow {
    public  static void closeWindow(BestQSystems ref){
        WindowEvent widnowEvent = new WindowEvent(ref, WindowEvent.WINDOW_CLOSING);
    }
}