尝试使用 Java 编程在不同的类中访问此关键字时遇到问题。我已经尝试过 Context、class.this 但还没有帮助...
我已经使用 NetBeans gui 构建器创建了一个项目,我希望当我单击按钮时表单得到处理...
主类包含处理JFrame Form的点击事件 BestQSystems.java:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
CloseWindow.closeWindow();
}
关闭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);
请告诉我如何在不同的类中访问 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);
}
}
关于java - 如何在不同的类中访问此关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32421364/