java二、八、十进制计算器
时间: 2023-07-10 17:32:46 浏览: 141
下面是一个简单的Java二、八、十进制计算器,可以实现加、减、乘、除四则运算,同时支持二、八、十进制之间的相互转换:
```
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
private JFrame frame;
private JTextField inputField;
private JLabel resultLabel;
private JButton btnBin, btnOct, btnDec;
private Stack<String> stack;
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() {
stack = new Stack<String>();
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
inputField = new JTextField();
inputField.setBounds(10, 10, 414, 30);
frame.getContentPane().add(inputField);
inputField.setColumns(10);
resultLabel = new JLabel("");
resultLabel.setBounds(10, 50, 414, 30);
frame.getContentPane().add(resultLabel);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "0");
}
});
btn0.setBounds(10, 90, 50, 30);
frame.getContentPane().add(btn0);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "1");
}
});
btn1.setBounds(70, 90, 50, 30);
frame.getContentPane().add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "2");
}
});
btn2.setBounds(130, 90, 50, 30);
frame.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "3");
}
});
btn3.setBounds(190, 90, 50, 30);
frame.getContentPane().add(btn3);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "4");
}
});
btn4.setBounds(250, 90, 50, 30);
frame.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "5");
}
});
btn5.setBounds(310, 90, 50, 30);
frame.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "6");
}
});
btn6.setBounds(370, 90, 50, 30);
frame.getContentPane().add(btn6);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "7");
}
});
btn7.setBounds(10, 130, 50, 30);
frame.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "8");
}
});
btn8.setBounds(70, 130, 50, 30);
frame.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputField.setText(inputField.getText() + "9");
}
});
btn9.setBounds(130, 130, 50, 30);
frame.getContentPane().add(btn9);
JButton btnAdd = new JButton("+");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stack.push(inputField.getText());
stack.push("+");
inputField.setText("");
}
});
btnAdd.setBounds(190, 130, 50, 30);
frame.getContentPane().add(btnAdd);
JButton btnSub = new JButton("-");
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stack.push(inputField.getText());
stack.push("-");
inputField.setText("");
}
});
btnSub.setBounds(250, 130, 50, 30);
frame.getContentPane().add(btnSub);
JButton btnMul = new JButton("*");
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stack.push(inputField.getText());
stack.push("*");
inputField.setText("");
}
});
btnMul.setBounds(310, 130, 50, 30);
frame.getContentPane().add(btnMul);
JButton btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stack.push(inputField.getText());
stack.push("/");
inputField.setText("");
}
});
btnDiv.setBounds(370, 130, 50, 30);
frame.getContentPane().add(btnDiv);
JButton btnEq = new JButton("=");
btnEq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stack.push(inputField.getText());
int result = calculate();
resultLabel.setText(String.valueOf(result));
}
});
btnEq.setBounds(310, 170, 110, 30);
frame.getContentPane().add(btnEq);
btnBin = new JButton("2进制");
btnBin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = inputField.getText();
if (text.length() > 0) {
int value = Integer.parseInt(text);
inputField.setText(Integer.toBinaryString(value));
}
}
});
btnBin.setBounds(10, 170, 80, 30);
frame.getContentPane().add(btnBin);
btnOct = new JButton("8进制");
btnOct.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = inputField.getText();
if (text.length() > 0) {
int value = Integer.parseInt(text);
inputField.setText(Integer.toOctalString(value));
}
}
});
btnOct.setBounds(100, 170, 80, 30);
frame.getContentPane().add(btnOct);
btnDec = new JButton("10进制");
btnDec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = inputField.getText();
if (text.length() > 0) {
int value = Integer.parseInt(text, 8); // 以8进制解析整数
inputField.setText(Integer.toString(value));
}
}
});
btnDec.setBounds(190, 170, 80, 30);
frame.getContentPane().add(btnDec);
}
// 计算表达式的值
private int calculate() {
int result = 0;
while (!stack.isEmpty()) {
String op = stack.pop();
if (op.equals("+")) {
result += Integer.parseInt(stack.pop());
} else if (op.equals("-")) {
result -= Integer.parseInt(stack.pop());
} else if (op.equals("*")) {
result *= Integer.parseInt(stack.pop());
} else if (op.equals("/")) {
result /= Integer.parseInt(stack.pop());
}
}
return result;
}
}
```
注意:由于本程序采用了堆栈来实现运算,因此只支持单次运算,不能连续输入多个运算符进行复合运算。
阅读全文