如何在不同的 class 中访问此关键字

How do I access this keyword In a different class

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

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

Main class包含配置JFrame Form的点击事件 BestQSystems.java:

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

   }  

Class 关闭 JFrame:CloseWindow.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);
    }
}

这一行有错误 WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); 请告诉我如何在不同的 class.

中访问 this 关键字

您可以将对 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);
    }
}