我有一个相当简单的计算器,我正在尝试将键绑定到JButton。我是Java的新手,我不太了解。我知道它涉及ActionListener,但是我无法理解如何将它变成我现在拥有的东西。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class Calculator2 extends JFrame implements ActionListener {
// Declare the GUI objects and variables HERE
JTextField ansText;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, plus, minus, multi, div,
equal, clear;
JPanel p1, p2, p3;
Double val1 = 0.0, val2 = 0.0, answer = 0.0;
int operator = 0;
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
// GUI Creation Code goes HERE
ansText = new JTextField("", 7);
ansText.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
ansText.addKeyListener(new KeyAdapter() { //Allow only numbers in ansText
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
e.consume();
}
}
});
b1 = new JButton("1");
b1.addActionListener(this);
b2 = new JButton("2");
b2.addActionListener(this);
b3 = new JButton("3");
b3.addActionListener(this);
b4 = new JButton("4");
b4.addActionListener(this);
b5 = new JButton("5");
b5.addActionListener(this);
b6 = new JButton("6");
b6.addActionListener(this);
b7 = new JButton("7");
b7.addActionListener(this);
b8 = new JButton("8");
b8.addActionListener(this);
b9 = new JButton("9");
b9.addActionListener(this);
b0 = new JButton("0");
b0.addActionListener(this);
plus = new JButton("+");
plus.addActionListener(this);
minus = new JButton("-");
minus.addActionListener(this);
multi = new JButton("*");
multi.addActionListener(this);
div = new JButton("/");
div.addActionListener(this);
equal = new JButton("=");
equal.addActionListener(this);
clear = new JButton("C");
clear.addActionListener(this);
this.setLayout(new BorderLayout());
p1 = new JPanel();
this.add(p1, BorderLayout.NORTH);
p1.setLayout(new GridLayout(0, 1, 2, 2));
p1.add(ansText);
p2 = new JPanel();
this.add(p2, BorderLayout.CENTER);
p2.setLayout(new GridLayout(4, 3, 2, 2));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(plus);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(minus);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(multi);
p2.add(clear);
p2.add(b0);
p2.add(equal);
p2.add(div);
this.setSize(200, 250);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//Number input
if (e.getSource() == b1) {
ansText.setText(ansText.getText() + b1.getText());
}
else if (e.getSource() == b2) {
ansText.setText(ansText.getText() + b2.getText());
}
else if (e.getSource() == b3) {
ansText.setText(ansText.getText() + b3.getText());
}
else if (e.getSource() == b4) {
ansText.setText(ansText.getText() + b4.getText());
}
else if (e.getSource() == b5) {
ansText.setText(ansText.getText() + b5.getText());
}
else if (e.getSource() == b6) {
ansText.setText(ansText.getText() + b6.getText());
}
else if (e.getSource() == b7) {
ansText.setText(ansText.getText() + b7.getText());
}
else if (e.getSource() == b8) {
ansText.setText(ansText.getText() + b8.getText());
}
else if (e.getSource() == b9) {
ansText.setText(ansText.getText() + b9.getText());
}
else if (e.getSource() == b0) {
ansText.setText(ansText.getText() + b0.getText());
}
//Operator input
else if (e.getSource() == plus) {
operator = 1;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
else if (e.getSource() == minus) {
operator = 2;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
else if (e.getSource() == multi) {
operator = 3;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
else if (e.getSource() == div) {
operator = 4;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
//Misc
else if (e.getSource() == equal) {
val2 = Double.parseDouble(ansText.getText());
if (operator == 1) {
answer = val1 + val2;
ansText.setText("" + answer);
} else if (operator == 2) {
answer = val1 - val2;
ansText.setText("" + answer);
} else if (operator == 3) {
answer = val1 * val2;
ansText.setText("" + answer);
} else if (operator == 4) {
answer = val1 / val2;
ansText.setText("" + answer);
}
}
else if (e.getSource() == clear) {
ansText.setText("");
val1 = 0.0;
val2 = 0.0;
answer = 0.0;
operator = 0;
}
}
}
答案 0 :(得分:2)
How to Use Key Bindings教程描述了键绑定的详细信息。下面是一个简单的示例,说明如何将操作绑定到键盘和主键盘上的加号键:
JPanel panel = new JPanel();
JPanel panel = new JPanel();
panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "plus");
panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS,
InputEvent.SHIFT_MASK), "plus");
panel.getActionMap().put("plus", plusAction);
panel.add(button);
答案 1 :(得分:0)
你不希望有一个单独的类有几个内部类来完成你的所有工作。与大多数其他GUI系统一样,Java Swing基于MVC,模型 - 视图 - 控制器。
这里的Model是一个包含累加器当前值和存储在显示中的值的对象。如果您正在编写RPN计算器,模型对象将包含RPN堆栈。
视图将是Swing类:JTextField
和JButtons
。
Controller将是一个新对象,比如CalculatorController
,它可以完成实际工作。他们会在您ActionListener
上调用方法,而不是CalculatorController
添加,减去,乘以或除以数字,而该对象又会更新JTextField。
jedyobidan建议查看Oracle的文档是非常好的。您希望将关键笔划放入JPanel
本身的输入映射中,因为无论用户关注的面板位于何处,它们都应该应用。然后,操作地图将保留AbstractActions
上调用方法的CalculatorController
。
我一直认为Mac或其前身NeXT的GUI开发风格及其Interface Builder程序对于初学者来说要比Java Swing使用的风格好得多。