简体   繁体   English

从另一个类调用JFrame

[英]Call the JFrame from another class

i have simple JFRAME (create by Netbeans) and i want to call frame.setState(Frame.NORMAL) from another class. 我有一个简单的JFRAME(由Netbeans创建),我想从另一个类调用frame.setState(Frame.NORMAL) How to do call? 怎么打?

public class Myclass{

     public void Frame_normal{

       ...?
       ???frame.setState(Frame.NORMAL);

     }

}

Basicaly you create an attribute in your class and you give it the reference to your JFrame. 基本上,您可以在类中创建一个属性,并为其提供对JFrame的引用。

 public class Myclass{

      JFrame frame;

      public MyClass(JFrame aFrame){
           this.frame = aFrame;
      }

      public void Frame_normal{


           frame.setState(Frame.NORMAL);

      }

 }

 MyClass class = new Myclass(theJFrame);
 class.Frame_normal();

You can do the following 您可以执行以下操作

  1. Create the frame in the class where you want to call setState() function on it. 在您要在其上调用setState()函数的类中创建框架。
  2. Create a getter method on that class where you are actually creating the Frame. 在实际创建Frame的类上创建一个getter方法。 Then use this getter to get your JFrame and call your method on it. 然后使用此getter获取JFrame并在其上调用方法。
  3. Make the JFrame public.(Not recommended) 将JFrame公开。(不推荐)

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

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