java计算器2、8、10进制
时间: 2023-07-10 19:32:56 浏览: 45
好的,下面是一个Java计算器的示例代码,它支持2、8、10进制计算:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private String operator = "";
private double num1 = 0, num2 = 0;
private boolean decimal = false;
private int base = 10;
public Calculator() {
super("Calculator");
// Create the display field
display = new JTextField("0");
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().add(display, BorderLayout.NORTH);
// Create the button panel
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
// Add the number buttons
for (int i = 0; i <= 9; i++) {
JButton button = new JButton(Integer.toString(i, base));
button.addActionListener(this);
buttonPanel.add(button);
}
// Add the operator buttons
JButton addButton = new JButton("+");
addButton.addActionListener(this);
buttonPanel.add(addButton);
JButton subtractButton = new JButton("-");
subtractButton.addActionListener(this);
buttonPanel.add(subtractButton);
JButton multiplyButton = new JButton("*");
multiplyButton.addActionListener(this);
buttonPanel.add(multiplyButton);
JButton divideButton = new JButton("/");
divideButton.addActionListener(this);
buttonPanel.add(divideButton);
JButton equalsButton = new JButton("=");
equalsButton.addActionListener(this);
buttonPanel.add(equalsButton);
JButton clearButton = new JButton("C");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);
JButton decimalButton = new JButton(".");
decimalButton.addActionListener(this);
buttonPanel.add(decimalButton);
JButton binaryButton = new JButton("Bin");
binaryButton.addActionListener(this);
buttonPanel.add(binaryButton);
JButton octalButton = new JButton("Oct");
octalButton.addActionListener(this);
buttonPanel.add(octalButton);
JButton decimalBaseButton = new JButton("Dec");
decimalBaseButton.addActionListener(this);
buttonPanel.add(decimalBaseButton);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals("+") || action.equals("-") || action.equals("*") || action.equals("/")) {
operator = action;
num1 = Double.parseDouble(display.getText());
display.setText("0");
decimal = false;
} else if (action.equals("=")) {
num2 = Double.parseDouble(display.getText());
double result = 0;
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;
}
display.setText(Double.toString(result));
decimal = false;
} else if (action.equals("C")) {
display.setText("0");
decimal = false;
} else if (action.equals(".")) {
if (!decimal) {
display.setText(display.getText() + ".");
decimal = true;
}
} else if (action.equals("Bin")) {
base = 2;
display.setText(Integer.toString((int) num1, base));
} else if (action.equals("Oct")) {
base = 8;
display.setText(Integer.toString((int) num1, base));
} else if (action.equals("Dec")) {
base = 10;
display.setText(Double.toString(num1));
} else {
if (display.getText().equals("0")) {
display.setText(action);
} else {
display.setText(display.getText() + action);
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setSize(300, 300);
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.setVisible(true);
}
}
```
该计算器类使用Swing库来实现GUI界面,并实现了ActionListener接口以处理各种事件。它支持数字输入、四则运算、小数点输入、清除、二进制、八进制、十进制数的转换。
希望这个示例代码可以帮到您!