Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在其他类中访问此关键字_Java_Swing_Windowlistener - Fatal编程技术网

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

Java 如何在其他类中访问此关键字,java,swing,windowlistener,Java,Swing,Windowlistener,我在尝试使用Java编程在其他类中访问此关键字时遇到问题。我已经尝试了上下文,类。但还没有帮助 我已经使用NetBeans gui builder创建了一个项目,我希望当我单击按钮时,表单得到处理 Main类包含用于处理JFrame表单的click事件 BestQSystems.java: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

我在尝试使用Java编程在其他类中访问此关键字时遇到问题。我已经尝试了上下文,类。但还没有帮助

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

Main类包含用于处理JFrame表单的click事件 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);
    }
}
public class CloseWindow {
    public  static void closeWindow(BestQSystems ref){
        WindowEvent widnowEvent = new WindowEvent(ref, WindowEvent.WINDOW_CLOSING);
    }
}

我在这一行出现错误
WindowEvent widnowEvent=newwindowevent(这是WindowEvent.WINDOW\u关闭)
请告诉我如何在不同类中访问
关键字。

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

BestQSystems.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      CloseWindow.closeWindow(this); 
}
在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);
    }
}
public class CloseWindow {
    public  static void closeWindow(BestQSystems ref){
        WindowEvent widnowEvent = new WindowEvent(ref, WindowEvent.WINDOW_CLOSING);
    }
}

对不起,请你帮我一下好吗。。假设BestQSystems.java看起来像this@KarueBensonKarue只有当
BestQSystems
扩展
窗口
窗口
的子类时,此示例才有效。好的,我理解,非常感谢您的帮助。。。还有一个问题。。如果我想将
BestQSystems
引用从
CloseWindow.java
传递到另一个类,该怎么办。。。我的意思是,假设我有一个类Login,它包含登录用户的功能,所以在登录用户之后,表单将被释放。所以在BestQSystems.java中,我只调用了登录函数。。。。有什么要补充的吗here@KarueBensonKarue您也可以做同样的事情,只需更改类的构造函数以接受
BestQSystems
对象作为参数,或者更改此类的方法以接受这样的参数。它现在已经工作了。。。谢谢你的帮助。。。抱歉问得太多了,我是java编程的初学者。。。谢谢你的帮助。。我现在为知道一些新的东西而感到骄傲。。。