【问题标题】:Make the same general JButton do different things depending on its subclass使相同的通用 JButton 根据其子类做不同的事情
【发布时间】:2014-06-02 09:54:13
【问题描述】:

我有一个非常简单的银行帐户,用于练习我的 MVC 知识。但是我遇到了障碍。

由于存款和取款屏幕本质上是相同的,我几乎将所有这些都抽象为一个 BaseWindow 类。

这是 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;
    }
}

问题

由于我使用的是模型-视图-控制器模式,执行按钮的功能是在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??
            }
        };
    }

...

问题是,我想不出createExecuteButtonListener 中的内容,因为我不能简单地说model.deposit()model.withdraw(),因为我不知道在哪个窗口中单击了执行按钮。

据我所知,我不能在这里使用多态,因为侦听器没有被绑定在 DepositWindowWithdrawWindow 子类中,我不想在那里调用它们,因为这会违反模型视图-控制器。

那么困扰我的是,我如何告诉按钮监听器/控制器什么是正确的?

【问题讨论】:

  • 所以如果我理解正确,您是在使用一个按钮(未直接显示在您的问题中)来创建提款和存款窗口?为什么要将相同的 ActionListener createExecuteButtonListener 附加到“执行”按钮?
  • 您通过参数将窗口的上下文传递给创建侦听器方法,在您的情况下,上下文是一些通知当前窗口的常量
  • @CanadianDavid 存款和取款窗口是 BaseWindow 类的子类
  • 在你的图像中,它们看起来完全一样,所以当你可以调用 BaseWindow("Deposit")BaseWindow("Withdraw") 并简单地使用添加一个单独的 ActionListener 时,为什么你需要继承 BaseWindow 类到每个 BaseWindow 的 getExecuteButton()?

标签: java swing model-view-controller jbutton actionlistener


【解决方案1】:

使用Action“将功能和状态与组件分开。”让模型导出一个合适的DepositActionWithdrawAction。让控制器将每个动作与正确的 Execute 按钮相关联。部分相关示例引用here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2022-08-24
    相关资源
    最近更新 更多