简体   繁体   English

Java - 在处理继承和接口时调用Swing中的方法

[英]Java - Calling methods in Swing whilst dealing with Inheritance and Interfaces

My problem is quite hard to word but here is the basic outline: 我的问题很难说,但这里是基本的大纲:

I have an interface: 我有一个界面:

public interface TheInterface {
    /**
     * 
     * Returns a string
     */

    public String getStuff(); 



}

I have an abstract class that implements this interface: 我有一个实现此接口的抽象类:

public abstract class GenericClass implements TheInterface {

    public GenericClass() {
        // TODO Auto-generated constructor stub
    }



    @Override
    public String getStuff() {
        return "Random string";
    }


}

I then have a class that extends GenericClass 然后我有一个扩展GenericClass的类

public class GUIClass extends GenericClass {
    private myFrame  myNewFrame;
    public GUIClass() {
        super();
        myNewFrame = new myFrame();

    }

}

As you can see, the GenericClass has a frame: 如您所见,GenericClass有一个框架:

import javax.swing.JFrame;


public class myFrame extends JFrame {

    private myPanel topPanel;

    public myFrame() {

        topPanel= new myPanel(); 
        add(topPanel); 

         setSize(400,200); 
         //setLocation(200,200); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         setTitle("Test Program"); 
         setVisible(true); 
    }

}

And inside that frame is a panel which contains a label: 在该框架内是一个包含标签的面板:

import java.awt.GridLayout; import java.awt.GridLayout; import java.awt.Label; import java.awt.Label;

import javax.swing.JLabel; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPanel;

public class myPanel extends JPanel {

    private JLabel myLabel;

    public myPanel() {
        setLayout(new GridLayout(0,2));
        add (new Label("This label should contain the content of getStuff(): "));
        myLabel=new JLabel();
        add (myLabel);
    }

}

And what I want to do here is to call getStuff() from the GenericClass and have it displayed inside that label. 我想在这里做的是从GenericClass调用getStuff()并将其显示在该标签内。 However at the moment I have no access to it and it seems that my design is flawed. 但是目前我无法访问它,似乎我的设计存在缺陷。 I would appreciate it if anyone could help to rearrange or change this so that it would be possible to call that method in the label in the most efficient way without multiple cases of the same code. 我很感激,如果有人可以帮助重新排列或更改它,以便能够以最有效的方式在标签中调用该方法,而无需多个相同代码的情况。

Thanks. 谢谢。

You could use the Observer pattern: 您可以使用Observer模式:

public interface StuffObserver {
/**
 * 
 * Pass whatever you want, perhaps getStuff(),
 * but that method might be removed by the time we're done here
 * (depends on what else might need to query/track it without,
 *  an observer)
 */
private void onStuffChanged(String newStuff); 

} }

Your Panel class is now 您的Panel类现在是

public class myPanel extends JPanel implements StuffObserver

which contains 其中包含

private void onStuffChanged(String newStuff)
{
   Runnable changeText = new Runnable() {
       public void run() {
           myLabel.setText(newStuff);
       }
   };
   SwingUtilities.invokeLater(changeText);
}

make sure you have myLabel referencing the actual label you added to panel (your current code around that might not be what you want?) 确保你有myLabel引用你添加到面板的实际标签(你当前的代码可能不是你想要的?)

From here, have perhaps GenericClass or it's subclass GUIClass can have a List of StuffObservers (with methods to add or delete from) 从这里开始,或许GenericClass或它的子类GUIClass可以有一个StuffObservers列表(带有添加或删除的方法)

private List<StuffObservers> stuffObservers = new ArrayList<>();
public void addStuffObserver(StuffObserver ob)...
// looks familar? Same way Swing has addActionListener() on some components
public void deleteStuffObserver(StuffObserver ob)... 

GUIClass can simply call something like: GUIClass可以简单地调用类似的东西:

myNewFrame = new myFrame();
addStuffObserver(myNewFrame.getPanel());

Your GenericClass or GUIClass can also do the following whenever it changes what the outcome of getStuff() can be: 每当更改getStuff()的结果时,GenericClass或GUIClass也可以执行以下操作:

for (StuffObserver ob : stuffObservers)
{
    ob.onStuffChanged(someStringRepresentingWhatYouWouldChangeGetStuffTo);
}

And get rid of getStuff() now. 现在摆脱getStuff()。 Anytime you change the state that getStuff() would have returned, your JLabel will now auto update to display that data. 无论何时更改getStuff()返回的状态,JLabel现在都会自动更新以显示该数据。

I would suggest you construct and manage your GUI components directly in GUIClass instead of auto-managing them in custom subclasses. 我建议您直接在GUIClass中构建和管理GUI组件,而不是在自定义子类中自动管理它们。

public class GUIClass extends GenericClass {
    private JFrame frame;
    private JPanel panel;
    private JLabel label;

    public GUIClass() {
        super();
        initialisation();
        setLabelText(getStuff());
    }

    private void initialisation() {
        // Label
        this.label = new JLabel();
        this.label.setText(getStuff());

        // Panel
        this.panel = new JPanel();
        this.panel.setLayout(new GridLayout(0, 2));
        this.panel.add(this.label);

        // Frame
        this.frame = new JFrame();
        this.frame.add(this.panel);
        this.frame.setSize(400, 200);
        this.frame.setLocation(200, 200);
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.frame.setTitle("Test Program");
        this.frame.setVisible(true);
    }

    private void setLabelText(String text) {
        this.label.setText(text);
    }
}

This is a design suggestion, so I might have forgotten some elements from your original code, but I think you can get the idea! 这是一个设计建议,所以我可能忘记了原始代码中的一些元素,但我认为你可以得到这个想法!

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

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