用Java写一个可视化计算器程序
时间: 2023-04-02 21:03:55 浏览: 122
好的,我可以回答这个问题。要用Java写一个可视化计算器程序,可以使用Java Swing库来实现。首先,需要创建一个GUI窗口,然后在窗口中添加按钮和文本框。按钮可以用来输入数字和运算符,文本框用来显示计算结果。接着,需要编写计算逻辑,可以使用Java的数学库来实现基本的加减乘除运算。最后,将按钮和文本框与计算逻辑进行关联,使得用户输入的内容可以被正确地计算并显示在文本框中。这样就可以实现一个简单的可视化计算器程序了。
相关问题
编写一个Java可视化计算器程序
以下是一个简单的Java可视化计算器程序的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener {
private JFrame frame;
private JTextField textField;
private JButton[] numberButtons = new JButton[10];
private JButton[] functionButtons = new JButton[8];
private JButton addButton, subButton, mulButton, divButton;
private JButton decButton, equButton, delButton, clrButton;
private JPanel panel;
private Font myFont = new Font("Arial", Font.BOLD, 30);
private double num1 = 0, num2 = 0, result = 0;
private char operator;
public Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);
textField = new JTextField();
textField.setBounds(50, 25, 300, 50);
textField.setFont(myFont);
textField.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Del");
clrButton = new JButton("Clr");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
for (int i = 0; i < 8; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}
delButton.setBounds(50, 430, 145, 50);
clrButton.setBounds(205, 430, 145, 50);
panel = new JPanel();
panel.setBounds(50, 100, 300, 300);
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.setBackground(Color.black);
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(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);
frame.add(panel);
frame.add(delButton);
frame.add(clrButton);
frame.add(textField);
frame.setVisible(true);
}
public static void main(String[] args) {
Calculator calc = new Calculator();
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
}
if (e.getSource() == decButton) {
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() == equButton) {
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() == clrButton) {
textField.setText("");
}
if (e.getSource() == delButton) {
String str = textField.getText();
textField.setText("");
for (int i = 0; i < str.length() - 1; i++) {
textField.setText(textField.getText() + str.charAt(i));
}
}
}
}
```
运行该程序,将会出现一个带有数字按钮、运算符按钮、小数点按钮、等于号按钮、删除按钮和清除按钮的计算器。你可以按照自己的需要修改代码,让计算器更加完善。
Java代码实现可视化计算器程序
可以使用 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[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"", ".", "=", "+"
};
private double result = ;
private String operator = "";
private boolean start = true;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
textField = new JTextField(10);
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttons = new JButton[buttonLabels.length];
for (int i = ; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(textField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.matches("[-9.]")) {
if (start) {
textField.setText(command);
start = false;
} else {
textField.setText(textField.getText() + command);
}
} else if (command.matches("[*/\\-+]")) {
if (!start) {
calculate(Double.parseDouble(textField.getText()));
operator = command;
start = true;
} else {
operator = command;
}
} else if (command.equals("=")) {
if (!start) {
calculate(Double.parseDouble(textField.getText()));
start = true;
}
} else if (command.equals("C")) {
result = ;
operator = "";
start = true;
textField.setText("");
}
}
private void calculate(double number) {
switch (operator) {
case "+":
result += number;
break;
case "-":
result -= number;
break;
case "*":
result *= number;
break;
case "/":
result /= number;
break;
default:
result = number;
break;
}
textField.setText(Double.toString(result));
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个程序使用了一个文本框和一个按钮面板来实现计算器的界面。按钮面板使用了 GridLayout 布局管理器,将按钮按照 4 行 4 列的方式排列。每个按钮都添加了一个 ActionListener,当用户点击按钮时,程序会根据按钮的标签执行相应的操作。
程序使用了一个 result 变量来保存计算结果,一个 operator 变量来保存当前的运算符,一个 start 变量来表示是否需要清空文本框。当用户点击数字按钮时,程序会将数字添加到文本框中。当用户点击运算符按钮时,程序会根据当前的运算符和文本框中的数字计算结果,并将结果保存到 result 变量中。当用户点击等号按钮时,程序会根据当前的运算符和文本框中的数字计算结果,并将结果显示在文本框中。当用户点击清除按钮时,程序会将 result、operator 和文本框都清空。
这个程序只是一个简单的示例,还有很多可以改进的地方。例如,可以添加更多的运算符和功能,可以添加错误处理机制,可以美化界面等。
阅读全文