实现java计算器,支持2、8、10进制。切换进制时数据清空。用btnBin.setBounds。
时间: 2024-03-06 16:51:02 浏览: 26
好的,我可以帮你实现一个支持2、8、10进制切换的Java计算器,并且在切换进制时清空数据,并且用 `btnBin.setBounds` 来设置按钮位置。你可以按照以下步骤进行实现:
1. 创建一个名为 Calculator 的类,其中包含以下成员变量:
```
private int base; // 当前进制
private String input; // 用户输入的数据
private double result; // 计算结果
```
2. 创建一个名为 switchBase 的方法,用于切换进制并清空数据:
```
public void switchBase(int newBase) {
this.base = newBase;
this.input = "";
this.result = 0.0;
}
```
3. 创建一个名为 clear 的方法,用于清空数据:
```
public void clear() {
this.input = "";
this.result = 0.0;
}
```
4. 创建一个名为 input 的方法,用于处理用户输入数据:
```
public void input(String data) {
this.input += data;
}
```
5. 创建一个名为 calculate 的方法,用于计算结果:
```
public void calculate() {
switch (this.base) {
case 2:
this.result = Integer.parseInt(this.input, 2);
break;
case 8:
this.result = Integer.parseInt(this.input, 8);
break;
case 10:
this.result = Double.parseDouble(this.input);
break;
default:
System.out.println("Unsupported base!");
return;
}
this.input = String.valueOf(this.result);
}
```
6. 最后,你可以编写一个简单的测试程序来测试你的计算器:
```
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setSize(300, 400);
Calculator calc = new Calculator();
JTextField txtInput = new JTextField("");
txtInput.setBounds(10, 10, 270, 40);
JButton btnBin = new JButton("Bin");
btnBin.setBounds(10, 60, 80, 40);
btnBin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.switchBase(2);
calc.clear();
txtInput.setText("");
}
});
JButton btnOct = new JButton("Oct");
btnOct.setBounds(100, 60, 80, 40);
btnOct.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.switchBase(8);
calc.clear();
txtInput.setText("");
}
});
JButton btnDec = new JButton("Dec");
btnDec.setBounds(190, 60, 80, 40);
btnDec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.switchBase(10);
calc.clear();
txtInput.setText("");
}
});
JButton btnClear = new JButton("C");
btnClear.setBounds(10, 110, 80, 40);
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.clear();
txtInput.setText("");
}
});
JButton btnEquals = new JButton("=");
btnEquals.setBounds(100, 110, 80, 40);
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.calculate();
txtInput.setText(String.valueOf(calc.getResult()));
}
});
JButton btn0 = new JButton("0");
btn0.setBounds(10, 160, 80, 40);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.input("0");
txtInput.setText(txtInput.getText() + "0");
}
});
JButton btn1 = new JButton("1");
btn1.setBounds(100, 160, 80, 40);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.input("1");
txtInput.setText(txtInput.getText() + "1");
}
});
JButton btn2 = new JButton("2");
btn2.setBounds(190, 160, 80, 40);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.input("2");
txtInput.setText(txtInput.getText() + "2");
}
});
frame.add(txtInput);
frame.add(btnBin);
frame.add(btnOct);
frame.add(btnDec);
frame.add(btnClear);
frame.add(btnEquals);
frame.add(btn0);
frame.add(btn1);
frame.add(btn2);
frame.setLayout(null);
frame.setVisible(true);
}
```
这样,你就可以通过点击按钮进行计算器的使用了。希望这个例子对你有所帮助!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)