java计算器,支持2、8、10进制,采用btnBin.setBounds
时间: 2023-07-10 07:32:55 浏览: 58
好的,以下是一个采用 `btnBin.setBounds` 的 Java 计算器示例代码,支持 2、8、10 进制:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField tfNum1, tfNum2, tfResult;
private JLabel lblNum1, lblNum2, lblResult;
private JButton btnAdd, btnSub, btnMul, btnDiv;
private JButton btnBin, btnOct, btnDec;
public Calculator() {
setTitle("计算器");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
lblNum1 = new JLabel("操作数1:");
lblNum1.setBounds(20, 20, 80, 20);
add(lblNum1);
tfNum1 = new JTextField();
tfNum1.setBounds(100, 20, 100, 20);
add(tfNum1);
lblNum2 = new JLabel("操作数2:");
lblNum2.setBounds(20, 60, 80, 20);
add(lblNum2);
tfNum2 = new JTextField();
tfNum2.setBounds(100, 60, 100, 20);
add(tfNum2);
lblResult = new JLabel("结果:");
lblResult.setBounds(20, 100, 80, 20);
add(lblResult);
tfResult = new JTextField();
tfResult.setBounds(100, 100, 100, 20);
tfResult.setEditable(false);
add(tfResult);
btnAdd = new JButton("+");
btnAdd.setBounds(20, 140, 50, 30);
btnAdd.addActionListener(this);
add(btnAdd);
btnSub = new JButton("-");
btnSub.setBounds(80, 140, 50, 30);
btnSub.addActionListener(this);
add(btnSub);
btnMul = new JButton("*");
btnMul.setBounds(140, 140, 50, 30);
btnMul.addActionListener(this);
add(btnMul);
btnDiv = new JButton("/");
btnDiv.setBounds(200, 140, 50, 30);
btnDiv.addActionListener(this);
add(btnDiv);
btnBin = new JButton("二进制");
btnBin.setBounds(20, 180, 80, 30);
btnBin.addActionListener(this);
add(btnBin);
btnOct = new JButton("八进制");
btnOct.setBounds(110, 180, 80, 30);
btnOct.addActionListener(this);
add(btnOct);
btnDec = new JButton("十进制");
btnDec.setBounds(200, 180, 80, 30);
btnDec.addActionListener(this);
add(btnDec);
}
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(tfNum1.getText().trim(), 10);
int num2 = Integer.parseInt(tfNum2.getText().trim(), 10);
int result = 0;
if (e.getSource() == btnAdd) {
result = num1 + num2;
} else if (e.getSource() == btnSub) {
result = num1 - num2;
} else if (e.getSource() == btnMul) {
result = num1 * num2;
} else if (e.getSource() == btnDiv) {
if (num2 == 0) {
JOptionPane.showMessageDialog(this, "除数不能为0!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
result = num1 / num2;
} else if (e.getSource() == btnBin) {
tfNum1.setText(Integer.toBinaryString(num1));
tfNum2.setText(Integer.toBinaryString(num2));
} else if (e.getSource() == btnOct) {
tfNum1.setText(Integer.toOctalString(num1));
tfNum2.setText(Integer.toOctalString(num2));
} else if (e.getSource() == btnDec) {
tfNum1.setText(Integer.toString(num1));
tfNum2.setText(Integer.toString(num2));
}
tfResult.setText(Integer.toString(result));
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
该计算器采用了 `btnBin.setBounds` 方法来设置按钮的位置和大小。用户输入操作数后,可以通过选择不同的进制按钮来将操作数转换成对应的进制。用户选择计算操作后,程序会将操作数转换成十进制并进行计算,最后将结果显示在结果文本框中。如果用户选择除法操作并且除数为0,程序会弹出错误对话框提示用户。
相关推荐
![](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)