简体   繁体   English

如何在windowslistener方法内调用JFrame(类本身)?

[英]How do I call the JFrame (the class itself) inside a windowslistener method?

I want to reference the JFrame (which is the class itself) inside a WindowsListener method. 我想在WindowsListener方法中引用JFrame(这是类本身)。 Is there any way to do this? 有什么办法吗?

    diag_ap.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            this.setEnabled(true); //does not work
        }
    }); 

I expect to call the class frame and disable it so that the only thing that can be pressed is the JDialog box. 我希望调用该类框架并将其禁用,以便唯一可以按下的是JDialog框。

Using this keyword inside new WindowAdapter().windowClosing(event) method refers to the WindowAdapter object that you created. new WindowAdapter().windowClosing(event)方法中使用this关键字引用您创建的WindowAdapter对象。

To refer the object of the JFrame inside WindowAdapter, you should use MyJFrame.this . 要在WindowAdapter中引用JFrame的对象,应使用MyJFrame.this So, the code should be, 因此,代码应该是

diag_ap.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            MyJFrame.this.setEnabled(true); // replace MyJFrame with name of your JFrame
        }
    });

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

相关问题 如何调用静态类中存在的方法? - How do I call a method which is present inside a static class? 如何从另一个 class 内部调用 onItemClick 方法? - How do I call the onItemClick method from inside another class? 如何在同一个类中的方法内部正确调用方法? - How do I properly call a method inside a method inside the same class? java - 如何在一个类中的方法内进行循环以调用另一个类中的另一种方法n Java? - How do I make a loop inside a method in one class to call on another method in another class n Java? 我如何覆盖类对象本身的 finalize() 方法? - How do i override the finalize() method of the class object itself? 如何从类中调用JFrame? - How can i call a JFrame from a class? 类扩展JFrame时如何引用JFrame - How do I reference the JFrame when the class extends JFrame 如何从JFrame 2检索JFrame 1中设置的类字段 - How Do I Retrieve Class Fields Set in JFrame 1 from JFrame 2 如何在另一个类中调用一个类的main方法? - How do I call main method of one class inside another class? 我可以从扩展JFrame而不是JPanel的类中调用componentShown()方法吗? 如果是,怎么办? - Can I call componentShown() method from a class that extends JFrame instead of JPanel ? If yes, how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM