windowbuilder怎么写加减法计算器,请用代码实现
时间: 2023-04-10 07:01:40 浏览: 215
以下是一个简单的加减法计算器的代码,使用 WindowBuilder 开发:
```java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calculator extends JFrame {
private JPanel contentPane;
private JTextField textField;
private double num1, num2, result;
private String operator;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 214, 30);
contentPane.add(textField);
textField.setColumns(10);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "1");
}
});
btn1.setBounds(10, 50, 50, 50);
contentPane.add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "2");
}
});
btn2.setBounds(70, 50, 50, 50);
contentPane.add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "3");
}
});
btn3.setBounds(130, 50, 50, 50);
contentPane.add(btn3);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "4");
}
});
btn4.setBounds(10, 110, 50, 50);
contentPane.add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "5");
}
});
btn5.setBounds(70, 110, 50, 50);
contentPane.add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "6");
}
});
btn6.setBounds(130, 110, 50, 50);
contentPane.add(btn6);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "7");
}
});
btn7.setBounds(10, 170, 50, 50);
contentPane.add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "8");
}
});
btn8.setBounds(70, 170, 50, 50);
contentPane.add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "9");
}
});
btn9.setBounds(130, 170, 50, 50);
contentPane.add(btn9);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "0");
}
});
btn0.setBounds(70, 230, 50, 50);
contentPane.add(btn0);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
}
});
btnPlus.setBounds(190, 50, 50, 50);
contentPane.add(btnPlus);
JButton btnMinus = new JButton("-");
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
}
});
btnMinus.setBounds(190, 110, 50, 50);
contentPane.add(btnMinus);
JButton btnEquals = new JButton("=");
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
}
textField.setText(Double.toString(result));
}
});
btnEquals.setBounds(190, 170, 50, 110);
contentPane.add(btnEquals);
}
}
```
阅读全文