简体   繁体   中英

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. I have tried Context, class.this but no help yet...

I have created a project using NetBeans gui builder, I want when i click button the form to get disposed...

Main class contains the click event for disposing the JFrame Form BestQSystems.java:

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

   }  

Class to close the 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); Please advise me on how to access this keyword in a different class.

You can pass a reference to this to the other method. For example:

BestQSystems.java

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

and in the CloseWindow.java

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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