用java设计一个简易的计算器,要求如下:要使用java的GUI设计出计算器界面;通过界面按钮,可以进行加减乘除运算,并能讲结果显示在界面中;计算可以有小数点,和正负整数的计算;要有清零功能
时间: 2024-03-21 20:40:20 浏览: 63
设计一个图形界面的计算器,完成简单的算术运算-JAVA课程设计.doc
好的,我可以为您提供一个简单的Java计算器程序示例,如下所示:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener {
JFrame frame;
JTextField textField;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton, subButton, mulButton, divButton;
JButton decimalButton, equalsButton, clearButton, negativeButton;
JPanel panel;
double num1 = 0, num2 = 0, result = 0;
char operator;
Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setLayout(null);
textField = new JTextField();
textField.setBounds(30, 40, 240, 30);
textField.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decimalButton = new JButton(".");
equalsButton = new JButton("=");
clearButton = new JButton("C");
negativeButton = new JButton("+/-");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decimalButton;
functionButtons[5] = equalsButton;
functionButtons[6] = clearButton;
functionButtons[7] = negativeButton;
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
}
panel = new JPanel();
panel.setBounds(30, 100, 240, 240);
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decimalButton);
panel.add(numberButtons[0]);
panel.add(equalsButton);
panel.add(divButton);
for (int i = 0; i < 8; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(new Font("Verdana", Font.BOLD, 16));
functionButtons[i].setFocusable(false);
}
for (int i = 0; i < 10; i++) {
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(new Font("Verdana", Font.BOLD, 16));
numberButtons[i].setFocusable(false);
}
textField.setFont(new Font("Verdana", Font.PLAIN, 16));
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
frame.add(textField);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText() + String.valueOf(i));
}
}
if (e.getSource() == decimalButton) {
textField.setText(textField.getText() + ".");
}
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}
if (e.getSource() == equalsButton) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textField.setText(String.valueOf(result));
num1 = result;
}
if (e.getSource() == clearButton) {
textField.setText("");
}
if (e.getSource() == negativeButton) {
double temp = Double.parseDouble(textField.getText());
temp *= -1;
textField.setText(String.valueOf(temp));
}
}
}
```
这个Java计算器程序可以执行加、减、乘、除、清零、正负数、小数点和等于的操作。运行程序后,您将看到一个计算器界面,您可以通过按钮进行计算,并将结果显示在界面中。
阅读全文