swing 编写计算器
时间: 2023-12-08 22:05:13 浏览: 57
OK,关于Swing编写计算器,您可以参考Java的GUI编程,使用Swing框架来实现。Swing提供了很多常用的组件,例如JButton、JLabel、JTextField等。您可以通过布局管理器来控制组件的位置、大小、对齐方式等。实现一个计算器主要包括以下几个步骤:定义GUI界面、实现运算逻辑、响应用户操作。
相关问题
java使用swing编写计算器
下面是一个简单的Java Swing计算器的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JButton[] numberButtons;
private JButton[] functionButtons;
private JButton addButton, subButton, mulButton, divButton;
private JButton decimalButton, equalButton, clearButton;
private JPanel panel;
private double num1 = 0, num2 = 0, result = 0;
private char operator;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(350, 450);
setLocationRelativeTo(null);
textField = new JTextField();
textField.setPreferredSize(new Dimension(300, 50));
textField.setFont(new Font("Arial", Font.PLAIN, 24));
textField.setEditable(false);
numberButtons = new JButton[10];
for (int i = 0; i < numberButtons.length; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].setPreferredSize(new Dimension(90, 70));
numberButtons[i].setFont(new Font("Arial", Font.PLAIN, 24));
numberButtons[i].addActionListener(this);
}
functionButtons = new JButton[10];
for (int i = 0; i < functionButtons.length; i++) {
functionButtons[i] = new JButton();
functionButtons[i].setPreferredSize(new Dimension(90, 70));
functionButtons[i].setFont(new Font("Arial", Font.PLAIN, 24));
functionButtons[i].addActionListener(this);
}
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decimalButton = new JButton(".");
equalButton = new JButton("=");
clearButton = new JButton("C");
addButton.setPreferredSize(new Dimension(90, 70));
subButton.setPreferredSize(new Dimension(90, 70));
mulButton.setPreferredSize(new Dimension(90, 70));
divButton.setPreferredSize(new Dimension(90, 70));
decimalButton.setPreferredSize(new Dimension(90, 70));
equalButton.setPreferredSize(new Dimension(270, 70));
clearButton.setPreferredSize(new Dimension(90, 70));
addButton.setFont(new Font("Arial", Font.PLAIN, 24));
subButton.setFont(new Font("Arial", Font.PLAIN, 24));
mulButton.setFont(new Font("Arial", Font.PLAIN, 24));
divButton.setFont(new Font("Arial", Font.PLAIN, 24));
decimalButton.setFont(new Font("Arial", Font.PLAIN, 24));
equalButton.setFont(new Font("Arial", Font.PLAIN, 24));
clearButton.setFont(new Font("Arial", Font.PLAIN, 24));
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
decimalButton.addActionListener(this);
equalButton.addActionListener(this);
clearButton.addActionListener(this);
panel = new JPanel(new GridLayout(5, 4));
panel.setPreferredSize(new Dimension(300, 350));
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(mulButton);
panel.add(decimalButton);
panel.add(numberButtons[0]);
panel.add(equalButton);
panel.add(divButton);
panel.add(clearButton);
panel.add(functionButtons[1]);
panel.add(functionButtons[2]);
panel.add(functionButtons[3]);
add(textField, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < numberButtons.length; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
}
if (e.getSource() == decimalButton) {
textField.setText(textField.getText().concat("."));
}
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() == equalButton) {
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));
}
if (e.getSource() == clearButton) {
textField.setText("");
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器包含了基本的加、减、乘、除、小数点、等于和清除按钮。您可以根据需要进行修改和扩展。
阅读全文