简体   繁体   English

使相同的常规JButton根据其子类做不同的事情

[英]Make the same general JButton do different things depending on its subclass

I have a very simple Bank Account going on, that I built to practice my MVC knowledge. 我有一个非常简单的银行帐户,用来建立我的MVC知识。 However I've hit a snag. 但是我遇到了障碍。

Since the Deposit and Withdraw screens are essentially the same, I abstracted almost all of it to a BaseWindow class. 由于“存款”和“提款”屏幕本质上是相同的,因此我几乎将所有屏幕都抽象为BaseWindow类。

存款窗口退出窗口

This is the BaseWindow class: 这是BaseWindow类:

public class BaseWindow extends JFrame {

    private static final long serialVersionUID = 4741220023341218042L;

    JButton executeButton;

    public BaseWindow(String title){
        super(title);
        JPanel panel = new JPanel(new GridLayout(0, 3));
        JLabel amountLabel = new JLabel("Amount: "); 
        JTextField inputBox = new JTextField(15);
        executeButton = new JButton("Execute");
        JLabel newBalanceLabel = new JLabel("New Balance: "); 
        JLabel newBalance = new JLabel();
        panel.add(amountLabel);
        panel.add(inputBox);
        panel.add(executeButton);
        panel.add(newBalanceLabel);
        panel.add(newBalance);
        panel.add(new JLabel()); //Empty, for layout purposes
        add(panel);
        pack();
    }

    public JButton getExecuteButton(){
        return executeButton;
    }
}

The Problem 问题

Since I'm using the Model-View-Controller pattern, the function of the Execute button is determined in the Controller class, which is consists of the following: 由于我使用的是Model-View-Controller模式,因此Execute按钮的功能在Controller类中确定,该类包括以下内容:

...
public void startDepositWindow(){
        DepositWindow depositWindow = view.createDepositWindow();
        depositWindow.getExecuteButton().addActionListener(createExecuteButtonListener());
        depositWindow.run();
    }
    public void startWithdrawWindow(){
        WithdrawWindow withdrawWindow = view.createWithdrawWindow();
        withdrawWindow.getExecuteButton().addActionListener(createExecuteButtonListener());
        withdrawWindow.run();
    }

    //Button Action Listeners
    private ActionListener createDepositButtonListener(){
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startDepositWindow();
            }
        };
    }
    private ActionListener createWithdrawButtonListener(){
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startWithdrawWindow();
            }
        };
    }
    private ActionListener createExecuteButtonListener(){
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // WHAT GOES HERE??
            }
        };
    }

...

The problem is, I can't think of what goes in the createExecuteButtonListener because I can't simply say model.deposit() or model.withdraw() because I don't know which window the Execute button was clicked inside of. 问题是,我无法考虑createExecuteButtonListener因为我不能简单地说出model.deposit()model.withdraw()因为我不知道在其中单击了Execute按钮是哪个窗口。

As far as I know, I can't use Polymorphism here because the listener is not being bound inside the DepositWindow or WithdrawWindow subclasses and I don't want to call them there since that would violate Model-View-Controller. 据我所知,我不能在这里使用多态性,因为监听器未绑定在DepositWindowWithdrawWindow子类内,并且我不想在那儿调用它们,因为那样会违反Model-View-Controller。

So what's bothering me is, how do I tell the button listener/controller what is the right thing to do? 因此,令我困扰的是,如何告诉按钮监听器/控制器正确的做法是什么?

Use Action "to separate functionality and state from a component." 使用Action “将功能和状态与组件分开”。 Let the model export a suitable DepositAction and WithdrawAction . 让模型导出合适的DepositActionWithdrawAction Let the controller associate each action with the correct Execute button. 让控制器将每个动作与正确的“ 执行”按钮相关联。 Some related examples are cited here . 这里引用一些相关的例子。

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

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