用windowbuilder帮我写一个计算器
时间: 2023-08-13 16:06:28 浏览: 51
好的,请问使用哪种编程语言?我这里提供 Java 的实现方式。
首先,需要安装 WindowBuilder 插件,可以在 Eclipse 的 Marketplace 中搜索安装。安装好后,在 Eclipse 的菜单栏中找到 WindowBuilder 选项,选择新建一个 Swing 界面。
接下来,我们需要设计计算器的界面。在 WindowBuilder 中,可以通过拖拽控件的方式来设计界面。设计好界面后,我们需要添加控件的事件处理方法,使其具有计算器的基本功能。
以下是一个简单的计算器的实现代码,你可以在此基础上进行修改和优化。
```java
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Calculator {
private JFrame frame;
private JTextField textField;
private double firstNumber;
private String operator;
/**
* Launch the application.
*/
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();
}
}
});
}
/**
* Create the application.
*/
public Calculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 245, 333);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 231, 295);
frame.getContentPane().add(panel);
panel.setLayout(null);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10, 11, 208, 43);
panel.add(textField);
textField.setColumns(10);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "7");
}
});
btn7.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn7.setBounds(10, 65, 50, 50);
panel.add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "8");
}
});
btn8.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn8.setBounds(70, 65, 50, 50);
panel.add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "9");
}
});
btn9.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn9.setBounds(130, 65, 50, 50);
panel.add(btn9);
JButton btnDivide = new JButton("/");
btnDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
}
});
btnDivide.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnDivide.setBounds(190, 65, 50, 50);
panel.add(btnDivide);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "4");
}
});
btn4.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn4.setBounds(10, 126, 50, 50);
panel.add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "5");
}
});
btn5.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn5.setBounds(70, 126, 50, 50);
panel.add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "6");
}
});
btn6.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn6.setBounds(130, 126, 50, 50);
panel.add(btn6);
JButton btnMultiply = new JButton("*");
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
}
});
btnMultiply.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnMultiply.setBounds(190, 126, 50, 50);
panel.add(btnMultiply);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "1");
}
});
btn1.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn1.setBounds(10, 187, 50, 50);
panel.add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "2");
}
});
btn2.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn2.setBounds(70, 187, 50, 50);
panel.add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "3");
}
});
btn3.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn3.setBounds(130, 187, 50, 50);
panel.add(btn3);
JButton btnMinus = new JButton("-");
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
}
});
btnMinus.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnMinus.setBounds(190, 187, 50, 50);
panel.add(btnMinus);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "0");
}
});
btn0.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn0.setBounds(10, 248, 50, 50);
panel.add(btn0);
JButton btnDot = new JButton(".");
btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + ".");
}
});
btnDot.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnDot.setBounds(70, 248, 50, 50);
panel.add(btnDot);
JButton btnEquals = new JButton("=");
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double secondNumber = Double.parseDouble(textField.getText());
double result = 0;
switch (operator) {
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "/":
result = firstNumber / secondNumber;
break;
default:
break;
}
textField.setText(Double.toString(result));
}
});
btnEquals.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnEquals.setBounds(130, 248, 110, 50);
panel.add(btnEquals);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
}
});
btnPlus.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnPlus.setBounds(190, 248, 50, 50);
panel.add(btnPlus);
}
}
```
希望对你有所帮助!