如果我想将 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) {
}
}