用Java的GUI制作一个按钮式的计算器,实现加减乘除等于,清屏,实现十进制,八进制,二进制转换,并且设置一个帮助按钮点击显示“苏浩然3210421090”
时间: 2024-02-25 22:56:11 浏览: 127
好的,我会尽力回答你的问题。首先,你可以使用Java Swing框架来实现一个按钮式的计算器。下面是一个简单的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton btn_clear, btn_divide, btn_multiply, btn_minus, btn_plus, btn_equal, btn_bin, btn_oct, btn_dec, btn_help;
private JPanel panel;
private double num1, num2, result;
private String operator;
public Calculator() {
super("Calculator");
display = new JTextField(10);
display.setEditable(false);
btn_clear = new JButton("C");
btn_clear.addActionListener(this);
btn_divide = new JButton("/");
btn_divide.addActionListener(this);
btn_multiply = new JButton("*");
btn_multiply.addActionListener(this);
btn_minus = new JButton("-");
btn_minus.addActionListener(this);
btn_plus = new JButton("+");
btn_plus.addActionListener(this);
btn_equal = new JButton("=");
btn_equal.addActionListener(this);
btn_bin = new JButton("BIN");
btn_bin.addActionListener(this);
btn_oct = new JButton("OCT");
btn_oct.addActionListener(this);
btn_dec = new JButton("DEC");
btn_dec.addActionListener(this);
btn_help = new JButton("Help");
btn_help.addActionListener(this);
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
panel.add(btn_bin);
panel.add(btn_oct);
panel.add(btn_dec);
panel.add(btn_help);
panel.add(btn_clear);
panel.add(btn_divide);
panel.add(btn_multiply);
panel.add(btn_minus);
panel.add(new JButton("7"));
panel.add(new JButton("8"));
panel.add(new JButton("9"));
panel.add(btn_plus);
panel.add(new JButton("4"));
panel.add(new JButton("5"));
panel.add(new JButton("6"));
panel.add(btn_equal);
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
panel.add(new JButton("0"));
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(display, BorderLayout.NORTH);
container.add(panel, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn_clear) {
display.setText("");
} else if (e.getSource() == btn_divide) {
num1 = Double.parseDouble(display.getText());
operator = "/";
display.setText("");
} else if (e.getSource() == btn_multiply) {
num1 = Double.parseDouble(display.getText());
operator = "*";
display.setText("");
} else if (e.getSource() == btn_minus) {
num1 = Double.parseDouble(display.getText());
operator = "-";
display.setText("");
} else if (e.getSource() == btn_plus) {
num1 = Double.parseDouble(display.getText());
operator = "+";
display.setText("");
} else if (e.getSource() == btn_equal) {
num2 = Double.parseDouble(display.getText());
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("" + result);
} else if (e.getSource() == btn_bin) {
int decimal = Integer.parseInt(display.getText());
String binary = Integer.toBinaryString(decimal);
display.setText(binary);
} else if (e.getSource() == btn_oct) {
int decimal = Integer.parseInt(display.getText());
String octal = Integer.toOctalString(decimal);
display.setText(octal);
} else if (e.getSource() == btn_dec) {
String input = display.getText();
if (input.startsWith("0b")) {
int binary = Integer.parseInt(input.substring(2), 2);
display.setText("" + binary);
} else if (input.startsWith("0")) {
int octal = Integer.parseInt(input, 8);
display.setText("" + octal);
} else {
display.setText(input);
}
} else if (e.getSource() == btn_help) {
JOptionPane.showMessageDialog(this, "苏浩然3210421090");
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器有加减乘除等于、清屏、十进制、八进制、二进制转换和帮助按钮。你可以在这个代码的基础上继续添加和修改功能。希望对你有所帮助!
阅读全文