如果我想将其实现与GUI类分开,我在理解ActionListener时遇到了一些麻烦,我该怎么做?该程序是一个简单的计算器。我试图想到一个GUI类(Boundary),它只有按钮,然后是一个单独的类(Control)来处理所有的动作事件(按下按钮)。我可以创建一个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) {
}
}
答案 0 :(得分: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
. . .
根据建议,Action
会将所有内容分开保存,因此更具可读性。以下是使用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);
add
方法会将其转换为JButton
,并将其设置为侦听器。