簡體   English   中英

從其他類中的 JButton 操作調用方法

[英]Calling methods from JButton actions in other classes

我在這里面臨的問題是我不確定如何讓 JButton 操作導致調用上面許多類的方法並從自身中刪除。

為了給出 scope 和結構的一些想法,我正在開發一個滑雪勝地管理應用程序。 它具有 SkiRun 和 Lift 等類,這兩個類都在 SkiResort class 中使用。 SkiResort 擁有對這些執行操作的功能。 最初,我使用 ResortApp class 來執行(控制台)接口,接受用戶輸入並對 SkiResort 進行相應的修改。

我現在通過 Swing 庫添加一個 GUI,並且具有(顯然)JFrame 的復雜結構,其中包含許多 JPanel,每個 JPanel 包含更多 JPanel,其中一些包含 JButton。 這些都通過名為 MountainManager 的 class 進行管理,該模塊設置 JFrame 和組件,並且應該執行與 SkiResort 的接口。 我在這里面臨的真正問題是我不確定如何擁有它,以便在按下 JButton 時,例如,該調用可以傳遞回它包含的 MountainManager,以便能夠執行它應該執行的任何操作去表演。

在包含的圖表中,紅線是我想要實現的調用。 到目前為止,所有的 JFrames 和 Panels 等都被稱為新的,並且沒有任何功能。

圖,紅線是我們想要的

作為基本的簡單解決方案是將偵聽器路徑到目標Jbutton

import java.awt.event.ActionListener;
import javax.swing.*;   

public class MountainManager {

    private int counter = 0;

    public MountainManager() {
        ActionListener actionListener = e -> {
            System.out.println("Button presssed "+ ++counter +" times");
        };
        new View(actionListener);
    }

    public static void main(String[] args) {
        new MountainManager();
    }
}

class View{

    private final ActionListener actionListener;

    public View(ActionListener actionListener) {

        this.actionListener = actionListener;
        JFrame f = new JFrame("Ski Resort");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationByPlatform(true);
        f.add(new MainPanel());
        f.pack();
        f.setVisible(true);
    }

    class MainPanel extends JPanel{

        MainPanel() {
            JButton testButton = new JButton("Click");
            testButton.addActionListener(actionListener);
            add(testButton);
        }
    }
} 

使用MVC 模式可以實現更好的設計。 引入一個Model class,在ViewMountainManage (控制器)之間共享,並監聽Model的變化:

import javax.swing.*;
import javax.swing.event.ChangeListener;

public class MountainManager {

    public MountainManager() {

        Model model = new Model();
        model.setListener(e -> System.out.println("Button presssed "+ model.getClickCount() +" times"));
        new View(model);
    }

    public static void main(String[] args) {
        new MountainManager();
    }
}

class View{

    private final Model model;

    public View(Model model) {

        this.model = model;
        JFrame f = new JFrame("Ski Resort");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationByPlatform(true);
        f.add(new MainPanel());
        f.pack();
        f.setVisible(true);
    }

    class MainPanel extends JPanel{

        MainPanel() {
            JButton testButton = new JButton("Click");
            testButton.addActionListener(e->model.incrementClickCount());
            add(testButton);
        }
    }
}

class Model {

    private int clickCount=0;
    //todo: change to List<ChangeListener> to support multi listeners
    private ChangeListener listener;

    int getClickCount(){
        return clickCount;
    }

    void incrementClickCount(){
         clickCount++;
         notifyListener();
    }

    void notifyListener(){
        if(listener != null){
            listener.stateChanged(null);
        }
    }

    void setListener(ChangeListener listener) {
        this.listener = listener;
    }
}

在這里在線測試)

暫無
暫無

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

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