设计并实现一个类似于windows操作系统附件中自带的计算器的一款简单的计算器,要求界面美观,设计合理;带有常用的菜单并实现其功能;能完成加、减、乘、除等简单的计算,在下面用java写出其核心代码。
时间: 2024-05-12 12:18:22 浏览: 68
很高兴收到您的问题,以下是代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame {
private JPanel jPanel;
private JTextField jTextField;
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0, buttonAdd, buttonMinus, buttonMultiply, buttonDivide, buttonEqual, buttonDot, buttonClear;
private double firstNum;
private String operator;
public Calculator() {
initUI();
}
private void initUI() {
setLayout(new BorderLayout());
jPanel = new JPanel();
jPanel.setLayout(new GridLayout(5, 4, 5, 5));
jTextField = new JTextField("");
jTextField.setHorizontalAlignment(JTextField.RIGHT);
jTextField.setEditable(false);
buttonClear = new JButton("C");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
buttonAdd = new JButton("+");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
buttonMinus = new JButton("-");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonMultiply = new JButton("*");
buttonDot = new JButton(".");
button0 = new JButton("0");
buttonEqual = new JButton("=");
buttonDivide = new JButton("/");
buttonClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText("");
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "1");
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "2");
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "3");
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "4");
}
});
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "5");
}
});
button6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "6");
}
});
button7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "7");
}
});
button8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "8");
}
});
button9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "9");
}
});
button0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + "0");
}
});
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNum = Double.parseDouble(jTextField.getText());
operator = "+";
jTextField.setText("");
}
});
buttonMinus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNum = Double.parseDouble(jTextField.getText());
operator = "-";
jTextField.setText("");
}
});
buttonMultiply.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNum = Double.parseDouble(jTextField.getText());
operator = "*";
jTextField.setText("");
}
});
buttonDivide.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNum = Double.parseDouble(jTextField.getText());
operator = "/";
jTextField.setText("");
}
});
buttonDot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText() + ".");
}
});
buttonEqual.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double secondNum = Double.parseDouble(jTextField.getText());
if (operator.equals("+")) {
jTextField.setText(String.valueOf(firstNum + secondNum));
} else if (operator.equals("-")) {
jTextField.setText(String.valueOf(firstNum - secondNum));
} else if (operator.equals("*")) {
jTextField.setText(String.valueOf(firstNum * secondNum));
} else if (operator.equals("/")) {
jTextField.setText(String.valueOf(firstNum / secondNum));
}
operator = "";
}
});
jPanel.add(buttonClear);
jPanel.add(buttonDivide);
jPanel.add(buttonMultiply);
jPanel.add(buttonMinus);
jPanel.add(button7);
jPanel.add(button8);
jPanel.add(button9);
jPanel.add(buttonAdd);
jPanel.add(button4);
jPanel.add(button5);
jPanel.add(button6);
jPanel.add(buttonDot);
jPanel.add(button1);
jPanel.add(button2);
jPanel.add(button3);
jPanel.add(buttonEqual);
jPanel.add(button0);
add(jTextField, BorderLayout.NORTH);
add(jPanel, BorderLayout.CENTER);
setTitle("计算器");
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这是一个简单的计算器,可以进行加、减、乘、除和小数点运算,并且还具有清空和求和功能。希望能帮到你。
阅读全文