java设计一个简单计算器

//最简单的,效果如图
java设计一个简单计算器_第1张图片
优化版的在这里
java设计一个简单计算器_第2张图片
优化之后的java计算器

这是简单计算器的代码:

package count;

import java.awt.BorderLayout;
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;
import java.awt.Font;
import javax.swing.SwingConstants;

public class Calculator extends JFrame {
     

	private JPanel contentPane;
	private JTextField textField;
	private int operator = 0;
	String num1 = "", num2 = "";
	private double number1, number2;
	private boolean flag = false;
	double value;

	/**
	 * Launch the application.
	 */
	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();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */

	public Calculator() {
     
		setTitle("\u8BA1\u7B97\u5668");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 418, 388);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.BOLD, 30));
		textField.setText("0");
		textField.setHorizontalAlignment(SwingConstants.RIGHT);
		textField.setBounds(14, 24, 372, 59);
		contentPane.add(textField);
		textField.setColumns(10);

//数字部分

		JButton btn0 = new JButton("0");
		btn0.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("0");
					flag = false;
				} else {
     

					if (textField.getText().equals("0")) {
     
						textField.setText("0");
					} else
						textField.setText(textField.getText() + 0);

				}
			}
		});
		btn0.setFont(new Font("黑体", Font.BOLD, 15));
		btn0.setBounds(14, 260, 53, 43);
		contentPane.add(btn0);

		JButton btn1 = new JButton("1");
		btn1.setFont(new Font("黑体", Font.BOLD, 15));
		btn1.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("1");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("1");
					else
						textField.setText(textField.getText() + 1);
				}
			}
		});
		btn1.setBounds(14, 92, 53, 43);
		contentPane.add(btn1);

		JButton btn2 = new JButton("2");
		btn2.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("2");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("2");
					else
						textField.setText(textField.getText() + 2);
				}
			}
		});
		btn2.setFont(new Font("黑体", Font.BOLD, 15));
		btn2.setBounds(81, 92, 53, 43);
		contentPane.add(btn2);

		JButton btn3 = new JButton("3");
		btn3.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("3");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("3");
					else
						textField.setText(textField.getText() + 3);
				}
			}
		});
		btn3.setFont(new Font("黑体", Font.BOLD, 15));
		btn3.setBounds(148, 92, 53, 43);
		contentPane.add(btn3);

		JButton btn4 = new JButton("4");
		btn4.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("4");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("4");
					else
						textField.setText(textField.getText() + 4);
				}
			}
		});
		btn4.setFont(new Font("黑体", Font.BOLD, 15));
		btn4.setBounds(14, 148, 53, 43);
		contentPane.add(btn4);

		JButton btn5 = new JButton("5");
		btn5.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("5");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("5");
					else
						textField.setText(textField.getText() + 5);
				}
			}
		});
		btn5.setFont(new Font("黑体", Font.BOLD, 15));
		btn5.setBounds(81, 148, 53, 43);
		contentPane.add(btn5);

		JButton btn6 = new JButton("6");
		btn6.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("6");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("6");
					else
						textField.setText(textField.getText() + 6);
				}
			}
		});
		btn6.setFont(new Font("黑体", Font.BOLD, 15));
		btn6.setBounds(148, 148, 53, 43);
		contentPane.add(btn6);

		JButton btn7 = new JButton("7");
		btn7.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("7");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("7");
					else
						textField.setText(textField.getText() + 7);
				}
			}
		});
		btn7.setFont(new Font("黑体", Font.BOLD, 15));
		btn7.setBounds(14, 204, 53, 43);
		contentPane.add(btn7);

		JButton btn8 = new JButton("8");
		btn8.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("8");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("8");
					else
						textField.setText(textField.getText() + 8);
				}
			}
		});
		btn8.setFont(new Font("黑体", Font.BOLD, 15));
		btn8.setBounds(81, 204, 53, 43);
		contentPane.add(btn8);

		JButton btn9 = new JButton("9");
		btn9.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText("9");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("9");
					else
						textField.setText(textField.getText() + 9);
				}
			}
		});
		btn9.setFont(new Font("黑体", Font.BOLD, 15));
		btn9.setBounds(148, 204, 53, 43);
		contentPane.add(btn9);

		// 运算符
		JButton btnAdd = new JButton("+");
		btnAdd.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				num1 = textField.getText();
				operator = 1;
				flag = true;
			}
		});
		btnAdd.setFont(new Font("黑体", Font.BOLD, 15));
		btnAdd.setBounds(215, 92, 53, 43);
		contentPane.add(btnAdd);

		JButton btnsubtract = new JButton("-");
		btnsubtract.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				num1 = textField.getText();
				operator = 2;
				flag = true;
			}
		});
		btnsubtract.setFont(new Font("黑体", Font.BOLD, 15));
		btnsubtract.setBounds(215, 148, 53, 43);
		contentPane.add(btnsubtract);

		JButton btnmultiply = new JButton("*");
		btnmultiply.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				num1 = textField.getText();
				operator = 3;
				flag = true;
			}
		});
		btnmultiply.setFont(new Font("黑体", Font.BOLD, 15));
		btnmultiply.setBounds(215, 204, 53, 43);
		contentPane.add(btnmultiply);

		JButton btndivide = new JButton("/");
		btndivide.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				num1 = textField.getText();
				operator = 4;
				flag = true;
			}
		});
		btndivide.setFont(new Font("黑体", Font.BOLD, 15));
		btndivide.setBounds(215, 260, 53, 43);
		contentPane.add(btndivide);

		// 符号部分
		JButton btnpoint = new JButton(".");
		btnpoint.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				if (flag) {
     
					textField.setText(".");
					flag = false;
				} else {
     
					if (textField.getText().equals("0"))
						textField.setText("0.");
					else
						textField.setText(textField.getText() + ".");
				}
				flag = false;
			}

		});
		btnpoint.setFont(new Font("黑体", Font.BOLD, 15));
		btnpoint.setBounds(81, 260, 53, 43);
		contentPane.add(btnpoint);

		JButton btnC = new JButton("C");
		btnC.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				textField.setText("0");
			}
		});
		btnC.setFont(new Font("黑体", Font.BOLD, 15));
		btnC.setBounds(148, 260, 53, 43);
		contentPane.add(btnC);

		JButton btnequal = new JButton("=");
		btnequal.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				// num1 operator num2 =
				number1 = Double.parseDouble(num1);
				number2 = Double.parseDouble(textField.getText());
				if (operator == 1)
					value = number1 + number2;
				else if (operator == 2)
					value = number1 - number2;
				else if (operator == 3)
					value = number1 * number2;
				else if (operator == 4)
					value = number1 / number2;
				if ((int) value == value) {
     
					int exp1 = (int) value;
					textField.setText(exp1 + "");
				} else {
     
					String exp0 = String.valueOf(value);
					textField.setText(exp0);
				}
			}
		});
		btnequal.setFont(new Font("黑体", Font.PLAIN, 37));
		btnequal.setBounds(301, 97, 85, 206);
		contentPane.add(btnequal);
	}
}

基于windowbuilder设计的。只实现了简单的加减乘除以及复位功能,还存在很多缺点。
缺点有:
*无法进行多个数据的计算(只能准确进行一对数据的计算)
*界面还需要进一步美化。方法并没有进行封装,导致代码量较大。
*计算的数据有大小限制。
*还没有设计当出现错误时跳出的提示信息或窗口。(例如除数不能为0,小数点不能为多个等等)。

后面如果有空闲时间还会继续优化。
我的水平有限,如果有什么错误或者更好的建议希望大家给我留言和发消息,一起共同进步。:)

你可能感兴趣的:(java,javase)