如何让下面的计算器程序能添加实现键盘输入进行操作,请写的详细点,最好有代码和代码注释,谢谢 15

importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjava... import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private final String[] KEYS = { "7", "8", "9", "/", "4", "5", "6",
"*", "1", "2", "3", "-", "0", "+/-", ".", "+" };
private final String[] COMMAND = { "Backspace", "CE", "C" };
private final String[] M = { "MC", "MR", "MS", "=" };
private JButton keys[] = new JButton[KEYS.length];
private JButton commands[] = new JButton[COMMAND.length];
private JButton m[] = new JButton[M.length];
private JTextField resultText = new JTextField("0");
private boolean firstDigit = true;
private double resultNum = 0.0;
private String operator = "=";
private boolean operateValidFlag = true;
public Calculator(){
super();
init();
this.setBackground(Color.LIGHT_GRAY);
this.setTitle("计算器");
this.setLocation(300, 200);
this.setResizable(false);
this.pack();
}
private void init() {
resultText.setHorizontalAlignment(JTextField.RIGHT);
resultText.setEditable(false);
resultText.setBackground(Color.white);
JPanel calckeysPanel = new JPanel();
calckeysPanel.setLayout(new GridLayout(4, 4, 3, 3));
for (int i = 0; i < KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
calckeysPanel.add(keys[i]);
keys[i].setForeground(Color.blue);
}
keys[3].setForeground(Color.red);
keys[7].setForeground(Color.red);
keys[11].setForeground(Color.red);
keys[15].setForeground(Color.red);
JPanel commandsPanel = new JPanel();
commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
for (int i = 0; i < COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
commandsPanel.add(commands[i]);
commands[i].setForeground(Color.red);
}
JPanel calmsPanel = new JPanel();
calmsPanel.setLayout(new GridLayout(4, 1, 3, 3));
for (int i = 0; i < M.length; i++) {
m[i] = new JButton(M[i]);
calmsPanel.add(m[i]);
m[i].setForeground(Color.red);
}
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout(3, 4));
panel1.add("North", commandsPanel);
panel1.add("West", calckeysPanel);
panel1.add("Center", calmsPanel);
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.add("Center", resultText);
getContentPane().setLayout(new BorderLayout(3, 5));
getContentPane().add("North", top);
getContentPane().add("Center", panel1);
for (int i = 0; i < KEYS.length; i++) {
keys[i].addActionListener(this);
}
for (int i = 0; i < COMMAND.length; i++) {
commands[i].addActionListener(this);
}
for (int i = 0; i < M.length; i++) {
m[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals(COMMAND[0])){
handleBackspace();
} else if (label.equals(COMMAND[1])) {
resultText.setText("0");
} else if (label.equals(COMMAND[2])){
handleC();
} else if ("0123456789.".indexOf(label) >= 0) {
handleNumber(label);
// handlezero(zero);
} else {
handleOperator(label);
}
}
private void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i > 0) {
text = text.substring(0, i - 1);
if (text.length() == 0) {
resultText.setText("0");
firstDigit = true;
operator = "=";
} else {
resultText.setText(text);
}
}
}
private void handleNumber(String key) {
if (firstDigit) {
resultText.setText(key);
} else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)){
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
展开
 我来答
malinghao138
2010-11-09
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
resultText.setText(resultText.getText() + key);
}
firstDigit = false;
}

private void handleC() {
resultText.setText("0");
firstDigit = true;
operator = "=";
}
private void handleOperator(String key) {
if (operator.equals("/")) {
if (getNumberFromText() == 0.0){
operateValidFlag = false;
resultText.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("+")){
resultNum += getNumberFromText();
} else if (operator.equals("-")){
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
resultNum *= getNumberFromText();
} else if (operator.equals("+/-")){
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
resultNum = getNumberFromText();
}
if (operateValidFlag) {
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
operator = key;
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e){
}
return result;
}

public static void main(String args[]) {
Calculator calculator1 = new Calculator();
calculator1.setVisible(true);
calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这些是补充的问题,因为字数不够,这不是回答
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式