简体   繁体   English

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

[英]How do I access this keyword In a different class

am having a problem trying to access this keyword in a different class using Java programming.尝试使用 Java 编程在不同的类中访问此关键字时遇到问题。 I have tried Context, class.this but no help yet...我已经尝试过 Context, class.this 但还没有帮助......

I have created a project using NetBeans gui builder, I want when i click button the form to get disposed...我已经使用 NetBeans gui 构建器创建了一个项目,我希望当我单击表单按钮时进行处理...

Main class contains the click event for disposing the JFrame Form BestQSystems.java:主类包含用于处理 JFrame 表单 BestQSystems.java 的单击事件:

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

   }  

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

Am having an error in this line WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);在这一行中出现错误WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); Please advise me on how to access this keyword in a different class.请告诉我如何在不同的类中访问this关键字。

You can pass a reference to this to the other method.您可以将this的引用传递给其他方法。 For example:例如:

BestQSystems.java最佳QSystems.java

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

and in the CloseWindow.java并在 CloseWindow.java

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM