Java用swing组件编写一个计算机程序,功能包括加减乘除,开方,百分数,及倒数计算
时间: 2024-03-19 15:43:05 浏览: 80
用Java中的Swing写的计算器软件
好的,以下是一个使用Java Swing组件编写的计算器程序,包括加减乘除、开方、百分比和倒数等常用计算功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0;
private JButton buttonAdd, buttonSub, buttonMul, buttonDiv, buttonSqrt, buttonPercent, buttonReciprocal;
private JButton buttonDot, buttonEqual, buttonClear;
private double num1, num2, result;
private String operator;
public Calculator() {
// 设置标题
setTitle("计算器");
// 创建文本框
textField = new JTextField();
textField.setEditable(false);
// 创建数字按钮
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");
button0 = new JButton("0");
// 创建运算符按钮
buttonAdd = new JButton("+");
buttonSub = new JButton("-");
buttonMul = new JButton("*");
buttonDiv = new JButton("/");
buttonSqrt = new JButton("√");
buttonPercent = new JButton("%");
buttonReciprocal = new JButton("1/x");
// 创建其他按钮
buttonDot = new JButton(".");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
// 设置按钮的字体
Font buttonFont = new Font("宋体", Font.PLAIN, 20);
button1.setFont(buttonFont);
button2.setFont(buttonFont);
button3.setFont(buttonFont);
button4.setFont(buttonFont);
button5.setFont(buttonFont);
button6.setFont(buttonFont);
button7.setFont(buttonFont);
button8.setFont(buttonFont);
button9.setFont(buttonFont);
button0.setFont(buttonFont);
buttonAdd.setFont(buttonFont);
buttonSub.setFont(buttonFont);
buttonMul.setFont(buttonFont);
buttonDiv.setFont(buttonFont);
buttonSqrt.setFont(buttonFont);
buttonPercent.setFont(buttonFont);
buttonReciprocal.setFont(buttonFont);
buttonDot.setFont(buttonFont);
buttonEqual.setFont(buttonFont);
buttonClear.setFont(buttonFont);
// 创建面板
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
// 设置面板的布局
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
panel2.setLayout(new GridLayout(4, 3, 5, 5));
panel3.setLayout(new GridLayout(1, 2, 5, 5));
panel4.setLayout(new GridLayout(1, 2, 5, 5));
panel5.setLayout(new GridLayout(1, 4, 5, 5));
panel6.setLayout(new GridLayout(1, 2, 5, 5));
// 将组件添加到面板中
panel1.add(textField);
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(button0);
panel2.add(buttonDot);
panel2.add(buttonClear);
panel3.add(buttonSqrt);
panel3.add(buttonPercent);
panel4.add(buttonReciprocal);
panel4.add(buttonDiv);
panel5.add(buttonMul);
panel5.add(buttonSub);
panel5.add(buttonAdd);
panel5.add(buttonEqual);
panel6.add(buttonClear);
// 将面板添加到窗口中
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.EAST);
add(panel4, BorderLayout.WEST);
add(panel5, BorderLayout.SOUTH);
add(panel6, BorderLayout.SOUTH);
// 为按钮添加事件监听器
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);
button0.addActionListener(this);
buttonAdd.addActionListener(this);
buttonSub.addActionListener(this);
buttonMul.addActionListener(this);
buttonDiv.addActionListener(this);
buttonSqrt.addActionListener(this);
buttonPercent.addActionListener(this);
buttonReciprocal.addActionListener(this);
buttonDot.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
// 设置窗口的大小、位置、可见性和关闭方式
setSize(300, 400);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 处理按钮事件的方法
public void actionPerformed(ActionEvent e) {
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() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == buttonDot) {
if (textField.getText().indexOf(".") == -1) {
textField.setText(textField.getText() + ".");
}
} else if (e.getSource() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonAdd) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == buttonSub) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == buttonMul) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == buttonDiv) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == buttonSqrt) {
num1 = Double.parseDouble(textField.getText());
result = Math.sqrt(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonPercent) {
num1 = Double.parseDouble(textField.getText());
result = num1 / 100;
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonReciprocal) {
num1 = Double.parseDouble(textField.getText());
result = 1 / num1;
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
textField.setText(Double.toString(result));
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个程序通过创建多个面板,将各个组件添加到不同的面板中,实现了一个简单的计算器界面。在按钮的事件处理方法中,根据用户的点击操作,执行相应的计算,并将结果显示在文本框中。其中,sqrt、% 和 1/x 操作通过 Java 提供的 Math 类实现。
阅读全文