使用Java语言实现以下要求。编写一个简单的计算器程序,要求有菜单,能进行简单的加减乘除运算。要求: 1、完成计算器的界面设计; 2、运用事件处理机制实现计算器的各个功能
时间: 2023-06-14 09:04:22 浏览: 128
好的,以下是Java语言实现的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener {
JFrame frame;
JTextField textField;
JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonClear, buttonEquals;
double firstNumber = 0;
double secondNumber = 0;
String operation = "";
Calculator() {
frame = new JFrame("Calculator");
textField = new JTextField();
textField.setBounds(30, 40, 280, 30);
textField.setEditable(false);
button7 = new JButton("7");
button7.setBounds(40, 100, 50, 40);
button8 = new JButton("8");
button8.setBounds(110, 100, 50, 40);
button9 = new JButton("9");
button9.setBounds(180, 100, 50, 40);
buttonDivide = new JButton("/");
buttonDivide.setBounds(250, 100, 50, 40);
button4 = new JButton("4");
button4.setBounds(40, 170, 50, 40);
button5 = new JButton("5");
button5.setBounds(110, 170, 50, 40);
button6 = new JButton("6");
button6.setBounds(180, 170, 50, 40);
buttonMultiply = new JButton("*");
buttonMultiply.setBounds(250, 170, 50, 40);
button1 = new JButton("1");
button1.setBounds(40, 240, 50, 40);
button2 = new JButton("2");
button2.setBounds(110, 240, 50, 40);
button3 = new JButton("3");
button3.setBounds(180, 240, 50, 40);
buttonSubtract = new JButton("-");
buttonSubtract.setBounds(250, 240, 50, 40);
button0 = new JButton("0");
button0.setBounds(40, 310, 50, 40);
buttonClear = new JButton("C");
buttonClear.setBounds(110, 310, 50, 40);
buttonEquals = new JButton("=");
buttonEquals.setBounds(180, 310, 50, 40);
buttonAdd = new JButton("+");
buttonAdd.setBounds(250, 310, 50, 40);
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonAdd.addActionListener(this);
buttonSubtract.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonClear.addActionListener(this);
buttonEquals.addActionListener(this);
frame.add(textField);
frame.add(button0);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.add(button7);
frame.add(button8);
frame.add(button9);
frame.add(buttonAdd);
frame.add(buttonSubtract);
frame.add(buttonMultiply);
frame.add(buttonDivide);
frame.add(buttonClear);
frame.add(buttonEquals);
frame.setLayout(null);
frame.setVisible(true);
frame.setSize(350, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == button1) {
textField.setText(textField.getText() + "1");
} else if (e.getSource() == button2) {
textField.setText(textField.getText() + "2");
} else if (e.getSource() == button3) {
textField.setText(textField.getText() + "3");
} else if (e.getSource() == button4) {
textField.setText(textField.getText() + "4");
} else if (e.getSource() == button5) {
textField.setText(textField.getText() + "5");
} else if (e.getSource() == button6) {
textField.setText(textField.getText() + "6");
} else if (e.getSource() == button7) {
textField.setText(textField.getText() + "7");
} else if (e.getSource() == button8) {
textField.setText(textField.getText() + "8");
} else if (e.getSource() == button9) {
textField.setText(textField.getText() + "9");
} else if (e.getSource() == buttonAdd) {
firstNumber = Double.parseDouble(textField.getText());
operation = "+";
textField.setText("");
} else if (e.getSource() == buttonSubtract) {
firstNumber = Double.parseDouble(textField.getText());
operation = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
firstNumber = Double.parseDouble(textField.getText());
operation = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
firstNumber = Double.parseDouble(textField.getText());
operation = "/";
textField.setText("");
} else if (e.getSource() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonEquals) {
secondNumber = Double.parseDouble(textField.getText());
if (operation.equals("+")) {
double result = firstNumber + secondNumber;
textField.setText(String.valueOf(result));
} else if (operation.equals("-")) {
double result = firstNumber - secondNumber;
textField.setText(String.valueOf(result));
} else if (operation.equals("*")) {
double result = firstNumber * secondNumber;
textField.setText(String.valueOf(result));
} else if (operation.equals("/")) {
double result = firstNumber / secondNumber;
textField.setText(String.valueOf(result));
}
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器程序使用了Swing GUI库,创建了一个JFrame窗口和各种按钮。每个按钮都添加了一个ActionListener,根据用户的点击操作来执行相应的操作。在单击运算符按钮时,将第一个数字保存在firstNumber变量中,并将操作符保存在operation变量中。在单击等号按钮时,将第二个数字保存在secondNumber变量中,并根据操作符执行相应的数学运算。最后,将结果显示在文本框中。
阅读全文