编写一个模拟计算器的程序.要包括以下方法:加法,减法,乘法,除法.每个方法至少有两个参数.
来源:学生作业帮助网 编辑:作业帮 时间:2024/09/03 02:50:28
编写一个模拟计算器的程序.要包括以下方法:加法,减法,乘法,除法.每个方法至少有两个参数.
编写一个模拟计算器的程序.要包括以下方法:加法,减法,乘法,除法.每个方法至少有两个参数.
编写一个模拟计算器的程序.要包括以下方法:加法,减法,乘法,除法.每个方法至少有两个参数.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 计算器 extends JFrame implements ActionListener {
private final String[] KEYS = { "7", "8", "9", "÷", "sqrt", "4", "5", "6",
"×", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
private final String[] COMMAND = { "Backspace", "CE", "C" };
private final String[] M = { " ", "MC", "MR", "MS", "M+" };
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();
private boolean firstDigit = true;
private double resultNum = 0.0;
private String operator = "=";
static double mr;// 记忆的数字
private boolean operateValidFlag = true;
JPanel panel[]=new JPanel[4];
public 计算器(){
super("计算器");
init();
setBackground(Color.LIGHT_GRAY);
setResizable(false);
setLocation(588, 250);
setSize(378,214);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); //窗口不能直接关闭
setVisible(true);
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e){
if (JOptionPane.showConfirmDialog(null,"确定退出? ", "提示",2)==0){
System.exit(0);
}}});
}
private void init() {
setLayout(null);
for (int i = 0; i < 4; i++) {
panel[i]=new JPanel();
add(panel[i]);
}
panel[0].setLayout(new GridLayout(1,1,3,3));
panel[1].setLayout(new GridLayout(1,3,3,3));
panel[2].setLayout(new GridLayout(5,1,3,3));
panel[3].setLayout(new GridLayout(4,5,3,3));
resultText.setHorizontalAlignment(JTextField.RIGHT);
resultText.setAlignmentX(50);
resultText.setEditable(true);
resultText.setFont(new Font("宋体",Font.PLAIN,19));//设置字体
resultText.setBackground(Color.white);
panel[0].add(resultText);
for (int i = 0; i < KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
panel[3].add(keys[i]);
if (i%5-3==0){ keys[i].setForeground(Color.red); }
else{keys[i].setForeground(Color.black);}
keys[i].setVisible(true);
keys[i].setFont(new Font(null,Font.PLAIN,17));//设置字体
keys[i].addActionListener(this);
keys[i].setHorizontalAlignment(keys[i].CENTER);
keys[i].setBackground(new Color(255,236,233));
}
keys[4].setFont(new Font(null,Font.PLAIN,13));//设置字体
keys[19].setForeground(Color.red);
for (int i = 0; i < COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
panel[1].add(commands[i]);
commands[i].setForeground(Color.red);
commands[i].addActionListener(this);
}
commands[0].setFont(new Font(null,Font.PLAIN,12));//设置字体
for (int i = 0; i < M.length; i++) {
m[i] = new JButton(M[i]);
panel[2].add(m[i]);
m[i].setForeground(Color.red);
m[i].addActionListener(this);
}
panel[0].setBounds(2, 0, 370, 30);
panel[1].setBounds(74, 30, 298, 29);
panel[2].setBounds(2,30, 70, 150);
panel[3].setBounds(74,60, 300, 120);
validate();
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals(COMMAND[0])){ //用户按了"Backspace"键
handleBackspace();
} else if (label.equals(COMMAND[1])) { //用户按了"CE"键
resultText.setText("0");
} else if (label.equals(COMMAND[2])){ //用户按了"C"键
handleC();
} else if (label.equals(M[4])){ //用户按了"M+"键
mr=mr+Double.valueOf(resultText.getText()).doubleValue();
if (mr==0.0){m[0].setText("");}
else {m[0].setText("M");}
} else if (label.equals(M[3])){ //用户按了"MS"键
mr=Double.valueOf(resultText.getText()).doubleValue();
if (mr==0.0){m[0].setText("");}
} else if (label.equals(M[2])){ //用户按了"MR"键
resultText.setText(""+mr);
} else if (label.equals(M[1])){ //用户按了"MC"键
mr=0.0;
m[0].setText("");
} else if (label.equals("sqrt")) { //平方根运算
resultNum = Math.sqrt(Double.valueOf(resultText.getText()).doubleValue());
resultText.setText(String.valueOf(resultNum));
} else if (label.equals("%")){ //百分号运算,除以100
resultNum = Double.valueOf(resultText.getText()).doubleValue() / 100;
resultText.setText(String.valueOf(resultNum));
} else if (label.equals("+/-")){ //正数负数运算
resultNum = Double.valueOf(resultText.getText()).doubleValue() * (-1);
resultText.setText(String.valueOf(resultNum));
} else if (label.equals("1/x")) { //倒数运算
resultNum=Double.valueOf(resultText.getText()).doubleValue();
if (resultNum == 0.0){ //操作不合法
operateValidFlag = false;
resultText.setText("零没有倒数");
} else {
resultNum = 1 / resultNum;
}
resultText.setText(String.valueOf(resultNum));
} else if ("0123456789.".indexOf(label) >= 0) { //用户按了数字键或者小数点键
handleNumber(label);
} else { //用户按了运算符键
handleOperator(label);
}
}
private void handleBackspace() { // 处理Backspace键被按下的事件
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(".")) { //如果输入的不是小数点,则将数字附在结果文本框的后面
resultText.setText(resultText.getText() + key);
}
firstDigit = false; //以后输入的肯定不是第一个数字了
}
private void handleC() { //处理C键被按下的事件, 初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "=";
}
private void handleOperator(String key) { //处理运算符键被按下的事件
if (operator.equals("÷")) { //除法运算 ,如果当前结果文本框中的值等于0
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 = 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[]) {
new 计算器();
}
}