主题:求教高手小程序问题
最近老师留个JAVA小计算器。。完成+ - * / 的功能 要求可以键盘输入~~以下是我自己编写的一个程序。。但就是实现不了用键盘也可一实现输入0--9,且用小键盘进行 运算~~~请高手给改改。。。最后说明我哪做错了。。。。越明白越好。。本人很菜 谢谢!!!
import java.awt.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Calculator
implements ActionListener { //导入动作监听接口
//设计面板中的单位
JFrame frame;
JTextField textAnswer;
JPanel panel, panel1, panel2, panel3;
JMenuBar mainMenu;
JTextField textMemory;
JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状
JButton buttonBk, buttonC;
JButton button[];
JButton buttonMR, buttonMS;
JButton buttonDot, buttonAdd, buttonSub, buttonMul,
buttonDiv;
JButton buttonEqual;
DecimalFormat df; //设置数据输出精度
boolean clickable; //控制当前能否按键
double memoryd; //使用内存中存储的数字
int memoryi;
double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)
short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
String copy; //做复制用
//构造函数
public Calculator() {
clickable = true;
answerd = 0;
JFrame frame = new JFrame("欢迎使用吕景先的计算器");
df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
textAnswer = new JTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(new Color(255, 255, 255));
panel = new JPanel();
frame.getContentPane().add(panel);
panel1 = new JPanel();
panel2 = new JPanel();
panel.setLayout(new BorderLayout());
//设计整个面板
mainMenu = new JMenuBar();
panel.add(mainMenu, BorderLayout.NORTH);
panel.add(textAnswer, BorderLayout.CENTER);
panel.add(panel1, BorderLayout.SOUTH);
panel1.setLayout(new BorderLayout());
textMemory = new JTextField(3);
textMemory.setEditable(false);
textMemory.setBackground(new Color(217, 217, 217));
labelMemSpace = new JLabel(" ");
buttonBk = new JButton("退格(←)");
buttonBk.setForeground(new Color(255, 0, 0));
buttonC = new JButton("清除(Q)");
buttonC.setForeground(new Color(255, 0, 0));
buttonBk.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2, BorderLayout.NORTH);
panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel2.add(textMemory);
panel2.add(labelMemSpace);
panel2.add(buttonBk);
panel2.add(buttonC);
panel3 = new JPanel();
panel1.add(panel3, BorderLayout.CENTER);
button = new JButton[10];
panel3.setLayout(new GridLayout(5,3));
for (int i = 0; i < button.length; i++) {
button = new JButton(Integer.toString(i));
button.setForeground(new Color(0, 0, 255));
}
buttonMR = new JButton("显示(X)");
buttonMR.setForeground(new Color(255, 0, 0));
buttonMS = new JButton("记忆(j)");
buttonMS.setForeground(new Color(255, 0, 0));
buttonDot = new JButton(".");
buttonDot.setForeground(new Color(0, 0, 255));
buttonAdd = new JButton("+");
buttonAdd.setForeground(new Color(255, 0, 0));
buttonSub = new JButton("-");
buttonSub.setForeground(new Color(255, 0, 0));
buttonMul = new JButton("*");
buttonMul.setForeground(new Color(255, 0, 0));
buttonDiv = new JButton("/");
buttonDiv.setForeground(new Color(255, 0, 0));
buttonEqual = new JButton("=");
buttonEqual.setForeground(new Color(255, 0, 0));
//将所有行为与监听绑定
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel2.add(buttonMR);
buttonMR.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel2.add(buttonMS);
buttonMS.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
//设置各个按钮行为
public void actionPerformed(ActionEvent event) {
Object temp = event.getSource();
try {
//如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后
for (int i = 0; i <= 9; i++)
if (temp == button && clickable == true)
textAnswer.setText(textAnswer.getText() + Integer.toString(i));
//按下'.'按钮时,判断当前文本框内字符串中含不含'.',如果已含,则不允许再插入'.'
if (temp == buttonDot && clickable == true) {
boolean isDot = false;
if (textAnswer.getText().length() == 0)
isDot = true;
for (int i = 0; i < textAnswer.getText().length(); i++)
if ('.' == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
if (isDot == false)
textAnswer.setText(textAnswer.getText() + ".");
}
if ( (temp == buttonAdd || temp == buttonSub || temp == buttonMul ||
temp == buttonDiv) && clickable == true) {
//'+'操作
if (temp == buttonAdd) {
switch (prekey) {
case 0:
answerd += Double.parseDouble(textAnswer.getText());
break;
case 1:
answerd -= Double.parseDouble(textAnswer.getText());
break;
case 2:
answerd *= Double.parseDouble(textAnswer.getText());
break;
case 3:
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除数不能为零");
clickable = false;
}
else
answerd /= Double.parseDouble(textAnswer.getText());
break;
default:
answerd = Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey = key = 0;
}
//'-'操作
if (temp == buttonSub) {
switch (prekey) {
case 0:
answerd += Double.parseDouble(textAnswer.getText());
break;
case 1:
answerd -= Double.parseDouble(textAnswer.getText());
break;
case 2:
answerd *= Double.parseDouble(textAnswer.getText());
break;
case 3:
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除数不能为零");
clickable = false;
}
else
answerd /= Double.parseDouble(textAnswer.getText());
break;
default:
answerd = Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey = key = 1;
}
import java.awt.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Calculator
implements ActionListener { //导入动作监听接口
//设计面板中的单位
JFrame frame;
JTextField textAnswer;
JPanel panel, panel1, panel2, panel3;
JMenuBar mainMenu;
JTextField textMemory;
JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状
JButton buttonBk, buttonC;
JButton button[];
JButton buttonMR, buttonMS;
JButton buttonDot, buttonAdd, buttonSub, buttonMul,
buttonDiv;
JButton buttonEqual;
DecimalFormat df; //设置数据输出精度
boolean clickable; //控制当前能否按键
double memoryd; //使用内存中存储的数字
int memoryi;
double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)
short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
String copy; //做复制用
//构造函数
public Calculator() {
clickable = true;
answerd = 0;
JFrame frame = new JFrame("欢迎使用吕景先的计算器");
df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
textAnswer = new JTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(new Color(255, 255, 255));
panel = new JPanel();
frame.getContentPane().add(panel);
panel1 = new JPanel();
panel2 = new JPanel();
panel.setLayout(new BorderLayout());
//设计整个面板
mainMenu = new JMenuBar();
panel.add(mainMenu, BorderLayout.NORTH);
panel.add(textAnswer, BorderLayout.CENTER);
panel.add(panel1, BorderLayout.SOUTH);
panel1.setLayout(new BorderLayout());
textMemory = new JTextField(3);
textMemory.setEditable(false);
textMemory.setBackground(new Color(217, 217, 217));
labelMemSpace = new JLabel(" ");
buttonBk = new JButton("退格(←)");
buttonBk.setForeground(new Color(255, 0, 0));
buttonC = new JButton("清除(Q)");
buttonC.setForeground(new Color(255, 0, 0));
buttonBk.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2, BorderLayout.NORTH);
panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel2.add(textMemory);
panel2.add(labelMemSpace);
panel2.add(buttonBk);
panel2.add(buttonC);
panel3 = new JPanel();
panel1.add(panel3, BorderLayout.CENTER);
button = new JButton[10];
panel3.setLayout(new GridLayout(5,3));
for (int i = 0; i < button.length; i++) {
button = new JButton(Integer.toString(i));
button.setForeground(new Color(0, 0, 255));
}
buttonMR = new JButton("显示(X)");
buttonMR.setForeground(new Color(255, 0, 0));
buttonMS = new JButton("记忆(j)");
buttonMS.setForeground(new Color(255, 0, 0));
buttonDot = new JButton(".");
buttonDot.setForeground(new Color(0, 0, 255));
buttonAdd = new JButton("+");
buttonAdd.setForeground(new Color(255, 0, 0));
buttonSub = new JButton("-");
buttonSub.setForeground(new Color(255, 0, 0));
buttonMul = new JButton("*");
buttonMul.setForeground(new Color(255, 0, 0));
buttonDiv = new JButton("/");
buttonDiv.setForeground(new Color(255, 0, 0));
buttonEqual = new JButton("=");
buttonEqual.setForeground(new Color(255, 0, 0));
//将所有行为与监听绑定
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel2.add(buttonMR);
buttonMR.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel2.add(buttonMS);
buttonMS.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
//设置各个按钮行为
public void actionPerformed(ActionEvent event) {
Object temp = event.getSource();
try {
//如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后
for (int i = 0; i <= 9; i++)
if (temp == button && clickable == true)
textAnswer.setText(textAnswer.getText() + Integer.toString(i));
//按下'.'按钮时,判断当前文本框内字符串中含不含'.',如果已含,则不允许再插入'.'
if (temp == buttonDot && clickable == true) {
boolean isDot = false;
if (textAnswer.getText().length() == 0)
isDot = true;
for (int i = 0; i < textAnswer.getText().length(); i++)
if ('.' == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
if (isDot == false)
textAnswer.setText(textAnswer.getText() + ".");
}
if ( (temp == buttonAdd || temp == buttonSub || temp == buttonMul ||
temp == buttonDiv) && clickable == true) {
//'+'操作
if (temp == buttonAdd) {
switch (prekey) {
case 0:
answerd += Double.parseDouble(textAnswer.getText());
break;
case 1:
answerd -= Double.parseDouble(textAnswer.getText());
break;
case 2:
answerd *= Double.parseDouble(textAnswer.getText());
break;
case 3:
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除数不能为零");
clickable = false;
}
else
answerd /= Double.parseDouble(textAnswer.getText());
break;
default:
answerd = Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey = key = 0;
}
//'-'操作
if (temp == buttonSub) {
switch (prekey) {
case 0:
answerd += Double.parseDouble(textAnswer.getText());
break;
case 1:
answerd -= Double.parseDouble(textAnswer.getText());
break;
case 2:
answerd *= Double.parseDouble(textAnswer.getText());
break;
case 3:
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除数不能为零");
clickable = false;
}
else
answerd /= Double.parseDouble(textAnswer.getText());
break;
default:
answerd = Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey = key = 1;
}