用Eclipse Java的windowbuilder做一个简单计算器。话不多说,直接上代码。
运行结果:
package 计算器;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Aaa {
private JFrame frame;
private JTextField textField;
String s = "";// 显示算式
double[] num = new double[100000];// 储存运算数据
char[] b = new char[100];// 储存运算符
int i = 0;// 输入运算数据和运算符是给数组计数
int c;// 计算结果时计数
double jieguo = 0;// 计算结果
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Aaa window = new Aaa();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Aaa() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("简易计算器");
frame.setBounds(100, 100, 622, 666);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(57, 62, 458, 105);
textField.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton button = new JButton("1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "1";
textField.setText(s);
num[i] = num[i] * 10 + 1;
}
});
button.setBounds(57, 217, 89, 60);
button.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button);
JButton button_1 = new JButton("2");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "2";
textField.setText(s);
num[i] = num[i] * 10 + 2;
}
});
button_1.setBounds(182, 217, 89, 60);
button_1.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_1);
JButton button_2 = new JButton("3");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "3";
textField.setText(s);
num[i] = num[i] * 10 + 3;
}
});
button_2.setBounds(299, 217, 89, 60);
button_2.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_2);
JButton button_3 = new JButton("4");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "4";
textField.setText(s);
num[i] = num[i] * 10 + 4;
}
});
button_3.setBounds(57, 316, 89, 60);
button_3.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_3);
JButton button_4 = new JButton("5");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "5";
textField.setText(s);
num[i] = num[i] * 10 + 5;
}
});
button_4.setBounds(182, 316, 89, 60);
button_4.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_4);
JButton button_5 = new JButton("6");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "6";
textField.setText(s);
num[i] = num[i] * 10 + 6;
}
});
button_5.setBounds(299, 316, 89, 60);
button_5.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_5);
JButton button_6 = new JButton("7");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "7";
textField.setText(s);
num[i] = num[i] * 10 + 7;
}
});
button_6.setBounds(57, 413, 89, 60);
button_6.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_6);
JButton button_7 = new JButton("8");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "8";
textField.setText(s);
num[i] = num[i] * 10 + 8;
}
});
button_7.setBounds(182, 413, 89, 60);
button_7.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_7);
JButton button_8 = new JButton("9");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "9";
textField.setText(s);
num[i] = num[i] * 10 + 9;
}
});
button_8.setBounds(299, 413, 89, 60);
button_8.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_8);
JButton button_9 = new JButton("0");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "0";
textField.setText(s);
num[i] = num[i] * 10 + 0;
}
});
button_9.setBounds(58, 503, 88, 60);
button_9.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_9);
JButton button_10 = new JButton("+");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "+";
textField.setText(s);
b[i] = '+';
i++;
}
});
button_10.setBounds(426, 217, 89, 60);
button_10.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_10);
JButton button_11 = new JButton("-");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "-";
textField.setText(s);
b[i] = '-';
i++;
}
});
button_11.setBounds(426, 316, 89, 60);
button_11.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_11);
JButton button_12 = new JButton("*");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "*";
textField.setText(s);
b[i] = '*';
i++;
}
});
button_12.setBounds(426, 413, 89, 60);
button_12.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_12);
JButton button_13 = new JButton("/");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = s + "/";
textField.setText(s);
b[i] = '/';
i++;
}
});
button_13.setBounds(426, 503, 89, 60);
button_13.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_13);
JButton btnC = new JButton("c");
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s = "";
textField.setText(s);// 清空文本框
for (c = 0; c < 100; c++) {
num[c] = 0;// 清空运算数据
b[c] = 0;// 清空运算符
}
i = 0;
c = 0;
jieguo = 0;
}
});
btnC.setBounds(182, 503, 89, 60);
btnC.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(btnC);
JButton button_14 = new JButton("=");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switch (b[0])// 为了方便用循环来计算,我想到的是先把第一次计算提出来
{
case '+': {
jieguo = num[0] + num[1];
break;
}
case '-': {
jieguo = num[0] - num[1];
break;
}
case '*': {
jieguo = num[0] * num[1];
break;
}
case '/': {
jieguo = num[0] / num[1];
break;
}
}
c++;
for (c = 1; c < i; c++) {
switch (b[c]) {
case '+': {
jieguo = jieguo + num[c + 1];
break;
}
case '-': {
jieguo = jieguo - num[c + 1];
break;
}
case '*': {
jieguo = jieguo * num[c + 1];
break;
}
case '/': {
jieguo = jieguo / num[c + 1];
break;
}
}
}
textField.setText(String.valueOf(jieguo));// .setText()的参数需要String,所以用String.valueOf()将long转换为String
}
});
button_14.setBounds(299, 503, 89, 60);
button_14.setFont(new Font("宋体", Font.BOLD, 25));
frame.getContentPane().add(button_14);
}
}