用JAVA语言写一个图形界面化复杂计算器
时间: 2023-04-10 17:03:57 浏览: 82
好的,我可以回答这个问题。要实现一个图形界面化复杂计算器,可以使用Java Swing库来创建GUI界面,然后使用Java的数学库来实现计算器的功能。可以使用文本框和按钮来实现用户输入和计算结果的显示,还可以添加菜单栏和工具栏来增强用户体验。具体实现细节可以参考Java Swing的官方文档和示例代码。
相关问题
用java语言写一个图形界面化计算器
好的,以下是一个简单的Java图形界面计算器的代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField displayField;
private JButton[] buttons;
private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
private JPanel buttonPanel;
private double num1, num2;
private String operator;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
displayField = new JTextField(30);
displayField.setEditable(false);
buttons = new JButton[buttonLabels.length];
buttonPanel = new JPanel(new GridLayout(4, 4));
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
getContentPane().add(displayField, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.matches("[0-9]+")) {
displayField.setText(displayField.getText() + command);
} else if (command.equals(".")) {
if (!displayField.getText().contains(".")) {
displayField.setText(displayField.getText() + ".");
}
} else if (command.matches("[+\\-*/=]")) {
if (operator == null) {
num1 = Double.parseDouble(displayField.getText());
operator = command;
displayField.setText("");
} else {
num2 = Double.parseDouble(displayField.getText());
switch (operator) {
case "+":
num1 += num2;
break;
case "-":
num1 -= num2;
break;
case "*":
num1 *= num2;
break;
case "/":
num1 /= num2;
break;
}
if (command.equals("=")) {
displayField.setText("" + num1);
operator = null;
} else {
operator = command;
displayField.setText("");
}
}
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
希望这个代码能够帮到你!
用java语言写一个图形界面化可以实现混合运算的计算器
好的,以下是一个简单的Java图形界面计算器的代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
private double operand1, operand2;
private String operator;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLayout(new BorderLayout());
display = new JTextField();
display.setEditable(false);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttons = new JButton[16];
for (int i = 0; i < 16; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals("C")) {
display.setText("");
} else if (label.equals("=")) {
operand2 = Double.parseDouble(display.getText());
double result = 0;
if (operator.equals("+")) {
result = operand1 + operand2;
} else if (operator.equals("-")) {
result = operand1 - operand2;
} else if (operator.equals("*")) {
result = operand1 * operand2;
} else if (operator.equals("/")) {
result = operand1 / operand2;
}
display.setText("" + result);
} else if (label.equals("+") || label.equals("-") || label.equals("*") || label.equals("/")) {
operand1 = Double.parseDouble(display.getText());
operator = label;
display.setText("");
} else {
display.setText(display.getText() + label);
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
这个计算器可以进行加、减、乘、除四种混合运算,支持小数点输入和清空操作。