设计并实现一个java的包含1-9的数字按钮和加减乘除基本运算功能的计算器应用程序。
时间: 2024-03-24 11:37:09 浏览: 13
好的,我可以为您提供一个简单的Java计算器应用程序的实现。以下是代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
// 定义计算器的各个部件
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, beq, bclr;
JLabel l;
JTextField tf;
// 定义计算器的构造方法
public Calculator() {
// 设置计算器的标题
setTitle("Calculator");
// 创建计算器的各个部件
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b0 = new JButton("0");
badd = new JButton("+");
bsub = new JButton("-");
bmul = new JButton("*");
bdiv = new JButton("/");
beq = new JButton("=");
bclr = new JButton("C");
l = new JLabel("Enter an expression:");
tf = new JTextField();
// 设置计算器的布局
JPanel p1 = new JPanel(new GridLayout(4, 3));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(badd);
p1.add(b0);
p1.add(bsub);
JPanel p2 = new JPanel(new GridLayout(4, 1));
p2.add(bmul);
p2.add(bdiv);
p2.add(beq);
p2.add(bclr);
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(l);
p3.add(tf);
JPanel p = new JPanel(new BorderLayout());
p.add(p3, BorderLayout.NORTH);
p.add(p1, BorderLayout.CENTER);
p.add(p2, BorderLayout.EAST);
// 将计算器的部件添加到窗口中
add(p);
// 注册事件监听器
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);
beq.addActionListener(this);
bclr.addActionListener(this);
// 设置计算器的大小和可见性
setSize(250, 200);
setVisible(true);
}
// 处理事件的方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
tf.setText(tf.getText() + "1");
} else if (e.getSource() == b2) {
tf.setText(tf.getText() + "2");
} else if (e.getSource() == b3) {
tf.setText(tf.getText() + "3");
} else if (e.getSource() == b4) {
tf.setText(tf.getText() + "4");
} else if (e.getSource() == b5) {
tf.setText(tf.getText() + "5");
} else if (e.getSource() == b6) {
tf.setText(tf.getText() + "6");
} else if (e.getSource() == b7) {
tf.setText(tf.getText() + "7");
} else if (e.getSource() == b8) {
tf.setText(tf.getText() + "8");
} else if (e.getSource() == b9) {
tf.setText(tf.getText() + "9");
} else if (e.getSource() == b0) {
tf.setText(tf.getText() + "0");
} else if (e.getSource() == badd) {
tf.setText(tf.getText() + "+");
} else if (e.getSource() == bsub) {
tf.setText(tf.getText() + "-");
} else if (e.getSource() == bmul) {
tf.setText(tf.getText() + "*");
} else if (e.getSource() == bdiv) {
tf.setText(tf.getText() + "/");
} else if (e.getSource() == beq) {
String expr = tf.getText();
try {
// 计算表达式的值
double result = eval(expr);
tf.setText(Double.toString(result));
} catch (Exception ex) {
// 处理计算错误的情况
tf.setText("Error");
}
} else if (e.getSource() == bclr) {
// 清空计算器的文本框
tf.setText("");
}
}
// 计算表达式的值
public static double eval(String expr) throws Exception {
String[] tokens = expr.split("(?<=[-+*/])|(?=[-+*/])");
double result = Double.parseDouble(tokens[0]);
for (int i = 1; i < tokens.length; i += 2) {
String op = tokens[i];
double operand = Double.parseDouble(tokens[i + 1]);
switch (op) {
case "+":
result += operand;
break;
case "-":
result -= operand;
break;
case "*":
result *= operand;
break;
case "/":
result /= operand;
break;
default:
throw new Exception("Invalid operator: " + op);
}
}
return result;
}
// 主函数
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器应用程序包含1-9的数字按钮和加减乘除基本运算功能,可以输入一个表达式并计算出结果。您可以根据需要进行修改和扩展。