1

如果我想将 ActionListener 的实现与我的 GUI 类分开,我很难理解它,我该怎么做?该程序是一个简单的计算器。我试图想到一个只有按钮的 GUI 类(边界),然后是一个单独的类(控制)来处理所有动作事件(按钮按下)。我可以创建一个 Control 类型的私有变量并让它以这种方式工作吗?如果您知道如何请帮助:)

控制类

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.*;


public class Boundary extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel northPanel;
    private JPanel southPanel;
    private Control control;

    public Boundary()
    {
        super("Calculator");

        getContentPane().setLayout(new BorderLayout());

        northPanel = new JPanel(); northPanel.setLayout(new BorderLayout());
        southPanel = new JPanel(); southPanel.setLayout(new GridLayout(4,4));
        control = new Control();

        JTextField numberField = new JTextField(20);
        JTextField operationField = new JTextField(1);

        JButton button1 = new JButton("1");
        JButton button2 = new JButton("2");
        JButton button3 = new JButton("3");
        JButton button4 = new JButton("4");
        JButton button5 = new JButton("5");
        JButton button6 = new JButton("6");
        JButton button7 = new JButton("7");
        JButton button8 = new JButton("8");
        JButton button9 = new JButton("9");
        JButton button0 = new JButton("0");
        JButton buttonequal = new JButton("=");
        JButton buttonclear = new JButton("C");
        JButton buttonplus = new JButton("+");
        JButton buttonminus = new JButton("-");
        JButton buttondivide = new JButton("/");
        JButton buttonmultiply = new JButton("*");

        button1.addActionListener(control);
        button2.addActionListener(control);
        button3.addActionListener(control);
        button4.addActionListener(control);
        button5.addActionListener(control);
        button6.addActionListener(control);
        button7.addActionListener(control);
        button8.addActionListener(control);
        button9.addActionListener(control);
        button0.addActionListener(control);
        buttonequal.addActionListener(control);
        buttonclear.addActionListener(control);
        buttonplus.addActionListener(control);
        buttonminus.addActionListener(control);
        buttondivide.addActionListener(control);
        buttonmultiply.addActionListener(control);


        northPanel.add(numberField, BorderLayout.CENTER);
        northPanel.add(operationField, BorderLayout.EAST);

        southPanel.add(button1);
        southPanel.add(button2);
        southPanel.add(button3);
        southPanel.add(buttondivide);
        southPanel.add(button4);
        southPanel.add(button5);
        southPanel.add(button6);
        southPanel.add(buttonmultiply);
        southPanel.add(button7);
        southPanel.add(button8);
        southPanel.add(button9);
        southPanel.add(buttonminus);
        southPanel.add(buttonclear);
        southPanel.add(button0);
        southPanel.add(buttonequal);
        southPanel.add(buttonplus);

        numberField.setVisible(true);
        numberField.setEditable(false);
        operationField.setVisible(true);
        operationField.setEditable(false);







        getContentPane().add(northPanel, BorderLayout.NORTH);
        getContentPane().add(southPanel, BorderLayout.CENTER);

        setLocation(300,300);
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

控制类

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;


public class Control implements ActionListener {

    private JButton button;

    Control()
    {

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {


    }

}
4

1 回答 1

1

在一系列if语句中,检查“操作命令”:

public void actionPerformed(ActionEvent evt) {
    String cmd = evt.getActionCommand();

    if("1".equals(cmd))
        // button1 was pressed
    else if("2".equals(cmd))
        // button2 was pressed
    . . .

如建议的那样, anAction会将所有内容分开保存,因此更具可读性。以下是如何使用Action(或它的子类AbstractAction):

Action button1 = new AbstractAction("1") {
    public void actionPerformed(ActionEvent evt){
        // button one pressed
    }
};
Action button2 = new AbstractAction("2") {
    public void actionPerformed(ActionEvent evt){
        // button two pressed
    }
};

southPanel.add(button1);
southPanel.add(button2);

The add method, will convert it to a JButton, and set it as the listener.

于 2013-03-14T01:51:05.213 回答