简体   繁体   English

访问第二类中另一类的Swing组件

[英]Accessing Swing components of another class within second class

I have 2 classes Engine.java and Window.java . 我有2个类Engine.javaWindow.java In the Window.java I have a button which will create instance of the Engine.java . Window.java我有一个按钮,它将创建Engine.java实例。

How can I pass Window.java to Engine.java ? 如何将Window.java传递给Engine.java

I know that I can use this, but this represent button at that moment of clicking button. 我知道我可以使用它,但是在单击按钮的那一刻,这个代表按钮。

The reason is that I want to have access to all component of Window.java within Engine.java . 原因是我想访问Window.javaEngine.java所有组件。

public class Engine{
   Window window;

   public Engine(Window en){
       window = en;
   }
   //rest of your code
}

public class Window(){

     btnDownload.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
                 //At the point where you create the Engine
                  Engine en = new Engine(this);
                 //rest of your code
        }
}
Engine en = new Engine(Window.this); 

The reason is that I want to have access to all component of Window.java within Engine.java 原因是我想访问Engine.java中Window.java的所有组件

Even if you do implement this, Engine will only be able to access those fields of Window which are declared public (unless they are in the same package, in which case, Engine will also be able to access all the default scoped Window methods). 即使您确实实现了此功能, Engine也将只能访问被声明为publicWindow那些字段(除非它们在同一程序包中,在这种情况下, Engine也将能够访问所有default作用域Window方法)。

Was going to suggest the Window.this, but if your only interested in the components, then why not: 会建议使用Window.this,但是如果您只对组件感兴趣,那为什么不呢?

public class Engine { 
    Component[] components; 

    public Engine(Component[] components){ 
        this.components = components;
    } 
}

public class Window() {

    JButton btnDownload;
    JButton btnUpload;

    public Window() {

        btnDownload = new JButton("Download");
        btnDownload.setName("Download");

        btnUpload = new JButton("Upload");
        btnUpload.setName("Upload");

        btnDownload.addMouseListener(new MouseAdapter() {  
            @Override  
            public void mouseClicked(MouseEvent e) {  
                   // create engine passing reference to only the components...
                   Engine en = new Engine( Window.this.getComponents() );  
                   // rest of your code  
            }  
    }

    public Component[] getComponents() {
        return new Component[] { btnDownload, btnUpload };
    }
}

Where Component is java.awt.Component, or you could use JComponent from swing. 其中Component是java.awt.Component,或者您可以从swing使用JComponent。

If you did something like this then I guess each component would only be meaningful by name. 如果您做了这样的事情,那么我猜每个组件的名称都只会有意义。 So you could use the setName method (shown in Window constructor) on each component and do something in the engine to access them by name where needed - which is perhaps bad. 因此,您可以在每个组件上使用setName方法(在Window构造函数中显示),并在引擎中执行某些操作以在需要的地方通过名称访问它们-这可能是不好的。 Either way, you want a reference to the different components in the engine. 无论哪种方式,您都需要引用引擎中的不同组件。 You could either use an additional class or interface to expose the components if the above example does not fit. 如果上述示例不合适,则可以使用其他类或接口来公开组件。

public class MyParams {
    private Component download;
    private Component upload;

    public MyParams(Component download, Component upload) {
        this.upload = upload;
        this.download = download;
    }

    public Component getUpload() {
        return this.upload;
    }

    public Component getDownload() {
        return this.download;
    }
}

Then when creating Engine: 然后在创建引擎时:

Engine e = new Engine( new MyParams( Window.this.btnDownload, Window.this.btnUpload) );

Engine Constructor: 引擎构造器:

public Engine(MyParams myParams) {
    this.myParams = myParams;
}

Access them in engine: 在引擎中访问它们:

this.myParams.getUpload().setText( "Engine class has changed me" );

Totally agree with Cuga about passing in the Window reference. 完全同意Cuga关于传递Window参考的意见。 This kind of goes against the good class design encapsulation techniques. 这种违背了良好的类设计封装技术。

Just food for thought 值得深思

this should work... 这应该工作...

public class Window(){

Windows ref = this;
btnDownload.addMouseListener(new MouseAdapter() {
        @Override
            public void mouseClicked(MouseEvent e) {
                       Engine en = new Engine(ref);
                 }

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

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