簡體   English   中英

Java:調用其他類中的方法,邏輯問題

[英]Java : calling method in other class, Logical issue

這是我的情況:

我有一節課:

分類:

Gui.java - 包括 GUI

save(){//here we change the GUI which is called in the Gui.java, depends on the arraylist, after that we refresh the GUI 

}

和一個類 popup.java

okbtn(){//here we add sth in the arraylist and call the methode save in the class GUI}

如果我打開彈出框並單擊 OK 按鈕,則調用 okbtn()。 但是如何更改特定 GUI.java 的 UI。

Gui.java : -Holds the UI like :private panel = new panel -save() // 刷新 UI for (String str : arraylist){ this.panel.addcontent(str))

Popupbox.java : Gui ui = new Gui();

添加某物。 到一個數組列表並調用方法 ui.save();

<--如果我執行 ui.save() 它不會改變我看到它改變新類的 UI 的 UI。

我是否必須向所有 GUI 元素添加公共靜態? 或者什么是最好的方法

“我是否必須向所有 GUI 元素添加公共靜態?:

不。把那個骯臟的想法從你的腦海中抹去。

相反,我會編寫代碼以提供GUI實現的業務interface 並且您可以將GUI的實例傳遞給Popup只需實現接口的方法,然后您將接口的實例(即 GUI)傳遞給 Popup 類。 就像是

public interface SomeInterface {
    public void save();
}

public class GUI implements SomeInterface {
    Popup popup = new Popup(this);

    @Override
    public void save() {
        // save
    }
}

public class Popup ...{
    private SomeInterface someInf;

    public Popup(SomeInterface someInf) {
        this.someInf = someInf;
    }
    ...
    public void actionPerformed(ActionEvent e) {
        someInf.save();
    }
}

暫無
暫無

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

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