创建一个Java可视化计算器的界面,并显示0到9的按钮与加减乘除按钮,只需要显示计算器的界面不需要使用,并显示输出结果
时间: 2024-02-01 13:15:32 浏览: 67
这是一个使用Java Swing库创建的简单可视化计算器界面,包含0到9的数字按钮和加减乘除等运算符号按钮。可以通过监听按钮事件,将用户输入的数字和运算符进行处理,最终输出计算结果。
```java
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
private JFrame frame;
private JTextField textField;
private double num1, num2, result;
private String operator;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Calculator() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
textField.setBounds(20, 20, 260, 50);
textField.setEditable(false);
frame.getContentPane().add(textField);
textField.setColumns(10);
JPanel panel = new JPanel();
panel.setBounds(20, 80, 260, 260);
panel.setLayout(new GridLayout(4, 4, 5, 5));
frame.getContentPane().add(panel);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "7");
}
});
panel.add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "8");
}
});
panel.add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "9");
}
});
panel.add(btn9);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
}
});
panel.add(btnPlus);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "4");
}
});
panel.add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "5");
}
});
panel.add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "6");
}
});
panel.add(btn6);
JButton btnMinus = new JButton("-");
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
}
});
panel.add(btnMinus);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "1");
}
});
panel.add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "2");
}
});
panel.add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "3");
}
});
panel.add(btn3);
JButton btnMultiply = new JButton("*");
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
}
});
panel.add(btnMultiply);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "0");
}
});
panel.add(btn0);
JButton btnDot = new JButton(".");
btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!textField.getText().contains(".")) {
textField.setText(textField.getText() + ".");
}
}
});
panel.add(btnDot);
JButton btnEqual = new JButton("=");
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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(Double.toString(result));
}
});
panel.add(btnEqual);
JButton btnDivide = new JButton("/");
btnDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
}
});
panel.add(btnDivide);
}
}
```
阅读全文