簡體   English   中英

Java - 在處理繼承和接口時調用Swing中的方法

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

我的問題很難說,但這里是基本的大綱:

我有一個界面:

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

    public String getStuff(); 



}

我有一個實現此接口的抽象類:

public abstract class GenericClass implements TheInterface {

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



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


}

然后我有一個擴展GenericClass的類

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

    }

}

如您所見,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); 
    }

}

在該框架內是一個包含標簽的面板:

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

import javax.swing.JLabel; 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);
    }

}

我想在這里做的是從GenericClass調用getStuff()並將其顯示在該標簽內。 但是目前我無法訪問它,似乎我的設計存在缺陷。 我很感激,如果有人可以幫助重新排列或更改它,以便能夠以最有效的方式在標簽中調用該方法,而無需多個相同代碼的情況。

謝謝。

您可以使用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); 

}

您的Panel類現在是

public class myPanel extends JPanel implements StuffObserver

其中包含

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

確保你有myLabel引用你添加到面板的實際標簽(你當前的代碼可能不是你想要的?)

從這里開始,或許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可以簡單地調用類似的東西:

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

每當更改getStuff()的結果時,GenericClass或GUIClass也可以執行以下操作:

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

現在擺脫getStuff()。 無論何時更改getStuff()返回的狀態,JLabel現在都會自動更新以顯示該數據。

我建議您直接在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);
    }
}

這是一個設計建議,所以我可能忘記了原始代碼中的一些元素,但我認為你可以得到這個想法!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM