用图形界面API,结合事件处理机制,编写Java Application程序,实现一个可视化的计算器(至少应具有按键和显示区,至少实现加、减、乘、除运算以及clear功能)
时间: 2023-12-03 07:45:41 浏览: 83
Java实现计算器,带界面
好的,以下是一个简单的可视化计算器的Java Application程序,实现了加、减、乘、除运算以及clear功能。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonClear, buttonEquals;
private double firstNum = 0.0;
private double secondNum = 0.0;
private char operation = ' ';
public Calculator() {
super("Calculator");
// create UI components
display = new JTextField(20);
display.setEditable(false);
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonClear = new JButton("C");
buttonEquals = new JButton("=");
// add action listeners to buttons
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);
// create layout and add components
JPanel panelTop = new JPanel(new FlowLayout());
panelTop.add(display);
JPanel panelMiddle = new JPanel(new GridLayout(4, 4));
panelMiddle.add(button7);
panelMiddle.add(button8);
panelMiddle.add(button9);
panelMiddle.add(buttonAdd);
panelMiddle.add(button4);
panelMiddle.add(button5);
panelMiddle.add(button6);
panelMiddle.add(buttonSubtract);
panelMiddle.add(button1);
panelMiddle.add(button2);
panelMiddle.add(button3);
panelMiddle.add(buttonMultiply);
panelMiddle.add(buttonClear);
panelMiddle.add(button0);
panelMiddle.add(buttonEquals);
panelMiddle.add(buttonDivide);
Container container = getContentPane();
container.add(panelTop, BorderLayout.NORTH);
container.add(panelMiddle, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 250);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
display.setText(display.getText() + "0");
} else if (e.getSource() == button1) {
display.setText(display.getText() + "1");
} else if (e.getSource() == button2) {
display.setText(display.getText() + "2");
} else if (e.getSource() == button3) {
display.setText(display.getText() + "3");
} else if (e.getSource() == button4) {
display.setText(display.getText() + "4");
} else if (e.getSource() == button5) {
display.setText(display.getText() + "5");
} else if (e.getSource() == button6) {
display.setText(display.getText() + "6");
} else if (e.getSource() == button7) {
display.setText(display.getText() + "7");
} else if (e.getSource() == button8) {
display.setText(display.getText() + "8");
} else if (e.getSource() == button9) {
display.setText(display.getText() + "9");
} else if (e.getSource() == buttonAdd) {
firstNum = Double.parseDouble(display.getText());
operation = '+';
display.setText("");
} else if (e.getSource() == buttonSubtract) {
firstNum = Double.parseDouble(display.getText());
operation = '-';
display.setText("");
} else if (e.getSource() == buttonMultiply) {
firstNum = Double.parseDouble(display.getText());
operation = '*';
display.setText("");
} else if (e.getSource() == buttonDivide) {
firstNum = Double.parseDouble(display.getText());
operation = '/';
display.setText("");
} else if (e.getSource() == buttonClear) {
firstNum = 0.0;
secondNum = 0.0;
operation = ' ';
display.setText("");
} else if (e.getSource() == buttonEquals) {
secondNum = Double.parseDouble(display.getText());
double result = 0.0;
switch (operation) {
case '+':
result = firstNum + secondNum;
break;
case '-':
result = firstNum - secondNum;
break;
case '*':
result = firstNum * secondNum;
break;
case '/':
result = firstNum / secondNum;
break;
}
display.setText("" + result);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
在这个程序中,我们使用了JFrame作为主窗口,JTextField作为显示区,JButton作为按键,并在其中添加了ActionListener来处理对应的事件。在按钮事件处理中,我们根据不同的按钮进行不同的操作,例如数字按钮将数字添加到显示区,运算符按钮记录第一个数字并记录要进行的运算符,等号按钮计算结果并显示在显示区。
为了简化代码,我们在这里只处理了double类型的数字,没有考虑其他类型的数字或其他错误情况。您可以根据需要进行修改和补充。
阅读全文