【Java】JavaSE算法之计算器GUI

在JavaSE的学习中,GUI图形界面虽然被社会放弃有一段时间了,但是GUI对于学习Java编程语言,巩固基础是一个比较好的学习技术,可以很好的巩固Java面向对象的综合运用。

今天分享使用Java的GUI实现PC端图形化计算器

【Java】JavaSE算法之计算器GUI_第1张图片   【Java】JavaSE算法之计算器GUI_第2张图片【Java】JavaSE算法之计算器GUI_第3张图片

 首先我们创建一个Calculate类,并创建该类的构造方法。

public class Calculate {

    public Calculate() {

    }

}

 接下来创建计算器的面板,并控制计算器的宽度和高度,并将其显示出来。

public class Calculate {

    private int frame_width = 285;
	private int frame_height = 392;

    public Calculate() {
        //创建面板
		JFrame frame = new JFrame("计算器");
        //设置宽度和高度
		frame.setSize(frame_width, frame_height);
        //允许关闭退出
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 禁止放大
		frame.setResizable(false);
		center(frame);
		addMenu(frame);
		// 添加主面板
		addmainpanel(frame);
        //将面板显示出来
		frame.setVisible(true);
	}

}

设置将GUI面板显示在PC端屏幕的中央位置

/**
	 * 设置窗口中心显示
	 * 
	 * @param frame
	 */
	public void center(JFrame frame) {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension dim = kit.getScreenSize();
		int x = (dim.width - frame_width) / 2;
		int y = (dim.height - frame_height) / 2;

		frame.setLocation(x, y);
	}

添加常用菜单

/**
	 * 添加菜单
	 * 
	 * @param frame
	 */
	public void addMenu(JFrame frame) {
		JMenuBar bar = new JMenuBar();

		JMenu vMenu = new JMenu("查看(V)");
		JMenu eMenu = new JMenu("编辑(E)");
		JMenu hMenu = new JMenu("帮助(H)");

		vMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		eMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		hMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));

		bar.add(vMenu);
		bar.add(eMenu);
		bar.add(hMenu);

		frame.setJMenuBar(bar);
	}

 接着创建主面板

/**
	 * 添加主面板
	 * 
	 * @param frame
	 */
	public void addmainpanel(JFrame frame) {
		JPanel panel = new JPanel();
		frame.setContentPane(panel);
		panel.setBackground(new Color(217, 228, 241));

		GridBagLayout layout = new GridBagLayout();
		frame.setLayout(layout);

		GridBagConstraints constraints = new GridBagConstraints();
		constraints.weightx = 5;
		constraints.weighty = 8;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor = GridBagConstraints.CENTER;

		addchildpanel(panel, constraints);
		addButton(panel, constraints);

	}

 再创建子面板

/**
	 * 子面板
	 * 
	 * @param mainpanel
	 * @param constraints
	 */
	public void addchildpanel(JPanel mainpanel, GridBagConstraints constraints) {
		JPanel panel = new JPanel();
		SpringLayout layout = new SpringLayout();
		panel.setLayout(layout);
		label = new JLabel("0");
		label2 = new JLabel("0");
		panel.setPreferredSize(new Dimension(frame_width - 20, 40));
		panel.setBackground(new Color(240, 245, 252));
		// 设置面板的边框
		panel.setBorder(BorderFactory.createLineBorder(new Color(142, 156, 173)));

		panel.add(label);
		panel.add(label2);

		layout.putConstraint(SpringLayout.EAST, label, -5, SpringLayout.EAST, panel);
		layout.putConstraint(SpringLayout.NORTH, label, 2, SpringLayout.NORTH, panel);
		layout.putConstraint(SpringLayout.NORTH, label2, 5, SpringLayout.SOUTH, label);
		layout.putConstraint(SpringLayout.EAST, label2, -5, SpringLayout.EAST, panel);

		label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		label2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));

		addCommpot(0, 0, 5, 2, constraints, mainpanel, panel);

	}

 将显示文本的组件添加至网格布局面板

/**
	 * 特殊网格布局的配置方法,添加组件
	 * 
	 * @param gridx
	 *            列的位置
	 * @param gridy
	 *            行的位置
	 * @param gridwidth
	 *            所跨的列数
	 * @param gridheight
	 *            所跨的行数
	 * @param constraints
	 *            配置文件类的对象
	 * @param mainpanel
	 *            主面板对象
	 * @param panel
	 *            组件的对象
	 */
	public void addCommpot(int gridx, int gridy, int gridwidth, int gridheight, GridBagConstraints constraints,
			JPanel mainpanel, JPanel panel) {
		constraints.gridx = gridx;
		constraints.gridy = gridy;
		constraints.gridwidth = gridwidth;
		constraints.gridheight = gridheight;
		mainpanel.add(panel, constraints);
	}

 接下来添加计算器按钮组件

/**
	 * 添加计算器的按键按钮组件
	 * 
	 * @param mainpanel
	 *            主面板
	 * @param constraints
	 *            配置文件对象
	 */
	public void addButton(JPanel mainpanel, GridBagConstraints constraints) {
		JButton b1 = new JButton("MC");
		JButton b2 = new JButton("MR");
		JButton b3 = new JButton("MS");
		JButton b4 = new JButton("M+");
		JButton b5 = new JButton("M-");
		JButton b6 = new JButton("←");
		JButton b7 = new JButton("CE");
		JButton b8 = new JButton("C");
		JButton b9 = new JButton("±");
		JButton b10 = new JButton("√");
		JButton b11 = new JButton("7");
		JButton b12 = new JButton("8");
		JButton b13 = new JButton("9");
		JButton b14 = new JButton("/");
		JButton b15 = new JButton("4");
		JButton b16 = new JButton("5");
		JButton b17 = new JButton("6");
		JButton b18 = new JButton("*");
		JButton b19 = new JButton("1/X");
		JButton b20 = new JButton("1");
		JButton b21 = new JButton("2");
		JButton b22 = new JButton("3");
		JButton b23 = new JButton("-");
		JButton b24 = new JButton("=");
		JButton b25 = new JButton("0");
		JButton b26 = new JButton(".");
		JButton b27 = new JButton("+");
		JButton b28 = new JButton("%");

		addCommpot(0, 2, 1, 1, constraints, mainpanel, b1);
		addCommpot(1, 2, 1, 1, constraints, mainpanel, b2);
		addCommpot(2, 2, 1, 1, constraints, mainpanel, b3);
		addCommpot(3, 2, 1, 1, constraints, mainpanel, b4);
		addCommpot(4, 2, 1, 1, constraints, mainpanel, b5);
		addCommpot(0, 3, 1, 1, constraints, mainpanel, b6);
		addCommpot(1, 3, 1, 1, constraints, mainpanel, b7);
		addCommpot(2, 3, 1, 1, constraints, mainpanel, b8);
		addCommpot(3, 3, 1, 1, constraints, mainpanel, b9);
		addCommpot(4, 3, 1, 1, constraints, mainpanel, b10);
		addCommpot(0, 4, 1, 1, constraints, mainpanel, b11);
		addCommpot(1, 4, 1, 1, constraints, mainpanel, b12);
		addCommpot(2, 4, 1, 1, constraints, mainpanel, b13);
		addCommpot(3, 4, 1, 1, constraints, mainpanel, b14);
		addCommpot(4, 4, 1, 1, constraints, mainpanel, b28);
		addCommpot(0, 5, 1, 1, constraints, mainpanel, b15);
		addCommpot(1, 5, 1, 1, constraints, mainpanel, b16);
		addCommpot(2, 5, 1, 1, constraints, mainpanel, b17);
		addCommpot(3, 5, 1, 1, constraints, mainpanel, b18);
		addCommpot(4, 5, 1, 1, constraints, mainpanel, b19);
		addCommpot(0, 6, 1, 1, constraints, mainpanel, b20);
		addCommpot(1, 6, 1, 1, constraints, mainpanel, b21);
		addCommpot(2, 6, 1, 1, constraints, mainpanel, b22);
		addCommpot(3, 6, 1, 1, constraints, mainpanel, b23);
		addCommpot(4, 6, 1, 2, constraints, mainpanel, b24);
		addCommpot(0, 7, 2, 1, constraints, mainpanel, b25);
		addCommpot(2, 7, 1, 1, constraints, mainpanel, b26);
		addCommpot(3, 7, 1, 1, constraints, mainpanel, b27);

		b1.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b2.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b3.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b4.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b5.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b6.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b7.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b8.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b9.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b10.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b11.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b12.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b13.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b14.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b15.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b16.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b17.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b18.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b19.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b20.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b21.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b22.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b23.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b24.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b25.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b26.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b27.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b28.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));

		buttonlistener(b1);
		buttonlistener(b2);
		buttonlistener(b3);
		buttonlistener(b4);
		buttonlistener(b5);
		buttonlistener(b6);
		buttonlistener(b7);
		buttonlistener(b8);
		buttonlistener(b9);
		buttonlistener(b10);
		buttonlistener(b11);
		buttonlistener(b12);
		buttonlistener(b13);
		buttonlistener(b14);
		buttonlistener(b15);
		buttonlistener(b16);
		buttonlistener(b17);
		buttonlistener(b18);
		buttonlistener(b19);
		buttonlistener(b20);
		buttonlistener(b21);
		buttonlistener(b22);
		buttonlistener(b23);
		buttonlistener(b24);
		buttonlistener(b25);
		buttonlistener(b26);
		buttonlistener(b27);
		buttonlistener(b28);

	}

 接下来将所有按钮组件添加至面板

/**
	 * 特殊网格布局的配置方法,添加组件
	 * 
	 * @param gridx
	 *            列的位置
	 * @param gridy
	 *            行的位置
	 * @param gridwidth
	 *            所跨的列数
	 * @param gridheight
	 *            所跨的行数
	 * @param constraints
	 *            配置文件类的对象
	 * @param mainpanel
	 *            主面板对象
	 * @param button
	 *            组件的对象
	 */
	public void addCommpot(int gridx, int gridy, int gridwidth, int gridheight, GridBagConstraints constraints,
			JPanel mainpanel, JButton button) {
		constraints.gridx = gridx;
		constraints.gridy = gridy;
		constraints.gridwidth = gridwidth;
		constraints.gridheight = gridheight;
		mainpanel.add(button, constraints);
	}

 给按钮添加点击事件

/**
	 * 添加按钮的点击事件
	 * 
	 * @param button
	 */
	public void buttonlistener(JButton button) {
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// 获取按钮的触发点击事件的文本
				String text = e.getActionCommand();
				if (text.equals("CE")) {
					// 用户按了"CE"键
					label2.setText("0");
					label.setText("0");
					firstKey = true;
					operator = "=";
					issqrt = true;
				} else if ("0123456789.".indexOf(text) >= 0) {
					// 用户按了数字键或者小数点键
					handleNumber(text);
					// handlezero(zero);
				} else {
					// 用户按了运算符键
					handleOperator(text);
				}

			}
		});
	}

判断用户按下的按钮是数字键还是运算符键以及等号键的执行算法处理

/**
	 * 处理数字键被按下的事件
	 *
	 * @param key
	 */
	private void handleNumber(String key) {
		if (firstKey) {
			// 输入的第一个数字
			if ((key.equals(".")) && (label2.getText().indexOf(".") < 0)) {
				label2.setText(label2.getText() + ".");
			} else {
				if (key.equals(".")) {
					label2.setText("0" + key);
				} else {
					label2.setText(key);
				}
			}
		} else if ((key.equals(".")) && (label2.getText().indexOf(".") < 0)) {
			// 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
			label2.setText(label2.getText() + ".");
		} else if (!key.equals(".")) {
			// 如果输入的不是小数点,则将数字附在结果文本框的后面
			label2.setText(label2.getText() + key);
		}
		// 以后输入的肯定不是第一个数字了
		firstKey = false;
	}

	String operator = "=";
	// 是否合法
	boolean operateValidFlag = true;
	// 计算中间 1+3-2
	private double resultNum = 0.0;
	private boolean issqrt = true;

	/**
	 * 处理运算符键被按下的事件
	 *
	 * @param key
	 */
	private void handleOperator(String key) {
		if (operator.equals("/")) {
			// 除法运算
			// 如果当前结果文本框中的值等于0
			if (getNumberFromText() == 0.0) {
				// 操作不合法
				operateValidFlag = false;
				label2.setText("除数不能为零");
			} else {
				resultNum /= getNumberFromText();
			}
		} else if (operator.equals("1/X")) {
			// 倒数运算
			if (resultNum == 0.0) {
				// 操作不合法
				operateValidFlag = false;
				label2.setText("零没有倒数");
			} else {
				resultNum = 1 / resultNum;
			}
		} else if (operator.equals("+")) {
			// 加法运算
			resultNum += getNumberFromText();
		} else if (operator.equals("-")) {
			// 减法运算
			resultNum -= getNumberFromText();
		} else if (operator.equals("*")) {
			// 乘法运算
			resultNum *= getNumberFromText();
		} else if (operator.equals("√")) {
			// 平方根运算
			if (resultNum < 0) {
				issqrt = false;
				label2.setText("无效输入");
				resultNum = 0;
			} else {
				resultNum = Math.sqrt(resultNum);
			}

		} else if (operator.equals("%")) {
			// 百分号运算,除以100
			resultNum = resultNum / 100;

		} else if (operator.equals("±")) {
			// 正数负数运算
			resultNum = resultNum * (-1);
		} else if (operator.equals("=")) {
			// 赋值运算
			resultNum = getNumberFromText();

		}
		if (operateValidFlag) {
			// 双精度浮点数的运算
			long t1;
			double t2;
			t1 = (long) resultNum;
			t2 = resultNum - t1;
			if (t2 == 0) {
				if (issqrt) {
					label2.setText(String.valueOf(t1));
				}
				if (key.equals("=")) {
					label.setText("0");
				} else {
					label.setText(label2.getText() + key);
				}
			} else {
				label2.setText(String.valueOf(resultNum));
				if (key.equals("=")) {
					label.setText("0");
				} else {
					label.setText(label2.getText() + key);
				}
			}
		}
		// 运算符等于用户按的按钮
		operator = key;
		firstKey = true;
		operateValidFlag = true;
	}

	/**
	 * 从结果文本框中获取数字
	 *
	 * @return
	 */
	private double getNumberFromText() {
		double result = 0;
		try {
			result = Double.valueOf(label2.getText()).doubleValue();
		} catch (NumberFormatException e) {
		}
		return result;
	}

最后在类中添加main方法,执行代码

public static void main(String[] args) {
		new Calculate();
	}

完整代码如下:

package com.xf.jsq;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class Calculate {
	private int frame_width = 285;
	private int frame_height = 392;
	private boolean firstKey = true;
	private StringBuilder builder = new StringBuilder();
	private JLabel label = null;
	private JLabel label2 = null;

	public Calculate() {
		JFrame frame = new JFrame("计算器");
		frame.setSize(frame_width, frame_height);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 禁止放大
		frame.setResizable(false);
		center(frame);
		addMenu(frame);
		// 添加主面板
		addmainpanel(frame);

		frame.setVisible(true);
	}

	/**
	 * 添加主面板
	 * 
	 * @param frame
	 */
	public void addmainpanel(JFrame frame) {
		JPanel panel = new JPanel();
		frame.setContentPane(panel);
		panel.setBackground(new Color(217, 228, 241));

		GridBagLayout layout = new GridBagLayout();
		frame.setLayout(layout);

		GridBagConstraints constraints = new GridBagConstraints();
		constraints.weightx = 5;
		constraints.weighty = 8;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor = GridBagConstraints.CENTER;

		addchildpanel(panel, constraints);
		addButton(panel, constraints);

	}

	/**
	 * 子面板
	 * 
	 * @param mainpanel
	 * @param constraints
	 */
	public void addchildpanel(JPanel mainpanel, GridBagConstraints constraints) {
		JPanel panel = new JPanel();
		SpringLayout layout = new SpringLayout();
		panel.setLayout(layout);
		label = new JLabel("0");
		label2 = new JLabel("0");
		panel.setPreferredSize(new Dimension(frame_width - 20, 40));
		panel.setBackground(new Color(240, 245, 252));
		// 设置面板的边框
		panel.setBorder(BorderFactory.createLineBorder(new Color(142, 156, 173)));

		panel.add(label);
		panel.add(label2);

		layout.putConstraint(SpringLayout.EAST, label, -5, SpringLayout.EAST, panel);
		layout.putConstraint(SpringLayout.NORTH, label, 2, SpringLayout.NORTH, panel);
		layout.putConstraint(SpringLayout.NORTH, label2, 5, SpringLayout.SOUTH, label);
		layout.putConstraint(SpringLayout.EAST, label2, -5, SpringLayout.EAST, panel);

		label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		label2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));

		addCommpot(0, 0, 5, 2, constraints, mainpanel, panel);

	}

	/**
	 * 添加计算器的按键按钮组件
	 * 
	 * @param mainpanel
	 *            主面板
	 * @param constraints
	 *            配置文件对象
	 */
	public void addButton(JPanel mainpanel, GridBagConstraints constraints) {
		JButton b1 = new JButton("MC");
		JButton b2 = new JButton("MR");
		JButton b3 = new JButton("MS");
		JButton b4 = new JButton("M+");
		JButton b5 = new JButton("M-");
		JButton b6 = new JButton("←");
		JButton b7 = new JButton("CE");
		JButton b8 = new JButton("C");
		JButton b9 = new JButton("±");
		JButton b10 = new JButton("√");
		JButton b11 = new JButton("7");
		JButton b12 = new JButton("8");
		JButton b13 = new JButton("9");
		JButton b14 = new JButton("/");
		JButton b15 = new JButton("4");
		JButton b16 = new JButton("5");
		JButton b17 = new JButton("6");
		JButton b18 = new JButton("*");
		JButton b19 = new JButton("1/X");
		JButton b20 = new JButton("1");
		JButton b21 = new JButton("2");
		JButton b22 = new JButton("3");
		JButton b23 = new JButton("-");
		JButton b24 = new JButton("=");
		JButton b25 = new JButton("0");
		JButton b26 = new JButton(".");
		JButton b27 = new JButton("+");
		JButton b28 = new JButton("%");

		addCommpot(0, 2, 1, 1, constraints, mainpanel, b1);
		addCommpot(1, 2, 1, 1, constraints, mainpanel, b2);
		addCommpot(2, 2, 1, 1, constraints, mainpanel, b3);
		addCommpot(3, 2, 1, 1, constraints, mainpanel, b4);
		addCommpot(4, 2, 1, 1, constraints, mainpanel, b5);
		addCommpot(0, 3, 1, 1, constraints, mainpanel, b6);
		addCommpot(1, 3, 1, 1, constraints, mainpanel, b7);
		addCommpot(2, 3, 1, 1, constraints, mainpanel, b8);
		addCommpot(3, 3, 1, 1, constraints, mainpanel, b9);
		addCommpot(4, 3, 1, 1, constraints, mainpanel, b10);
		addCommpot(0, 4, 1, 1, constraints, mainpanel, b11);
		addCommpot(1, 4, 1, 1, constraints, mainpanel, b12);
		addCommpot(2, 4, 1, 1, constraints, mainpanel, b13);
		addCommpot(3, 4, 1, 1, constraints, mainpanel, b14);
		addCommpot(4, 4, 1, 1, constraints, mainpanel, b28);
		addCommpot(0, 5, 1, 1, constraints, mainpanel, b15);
		addCommpot(1, 5, 1, 1, constraints, mainpanel, b16);
		addCommpot(2, 5, 1, 1, constraints, mainpanel, b17);
		addCommpot(3, 5, 1, 1, constraints, mainpanel, b18);
		addCommpot(4, 5, 1, 1, constraints, mainpanel, b19);
		addCommpot(0, 6, 1, 1, constraints, mainpanel, b20);
		addCommpot(1, 6, 1, 1, constraints, mainpanel, b21);
		addCommpot(2, 6, 1, 1, constraints, mainpanel, b22);
		addCommpot(3, 6, 1, 1, constraints, mainpanel, b23);
		addCommpot(4, 6, 1, 2, constraints, mainpanel, b24);
		addCommpot(0, 7, 2, 1, constraints, mainpanel, b25);
		addCommpot(2, 7, 1, 1, constraints, mainpanel, b26);
		addCommpot(3, 7, 1, 1, constraints, mainpanel, b27);

		b1.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b2.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b3.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b4.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b5.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b6.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b7.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b8.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b9.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b10.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b11.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b12.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b13.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b14.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b15.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b16.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b17.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b18.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b19.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b20.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b21.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b22.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b23.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b24.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b25.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b26.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b27.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		b28.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));

		buttonlistener(b1);
		buttonlistener(b2);
		buttonlistener(b3);
		buttonlistener(b4);
		buttonlistener(b5);
		buttonlistener(b6);
		buttonlistener(b7);
		buttonlistener(b8);
		buttonlistener(b9);
		buttonlistener(b10);
		buttonlistener(b11);
		buttonlistener(b12);
		buttonlistener(b13);
		buttonlistener(b14);
		buttonlistener(b15);
		buttonlistener(b16);
		buttonlistener(b17);
		buttonlistener(b18);
		buttonlistener(b19);
		buttonlistener(b20);
		buttonlistener(b21);
		buttonlistener(b22);
		buttonlistener(b23);
		buttonlistener(b24);
		buttonlistener(b25);
		buttonlistener(b26);
		buttonlistener(b27);
		buttonlistener(b28);

	}

	/**
	 * 添加按钮的点击事件
	 * 
	 * @param button
	 */
	public void buttonlistener(JButton button) {
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// 获取按钮的触发点击事件的文本
				String text = e.getActionCommand();
				if (text.equals("CE")) {
					// 用户按了"CE"键
					label2.setText("0");
					label.setText("0");
					firstKey = true;
					operator = "=";
					issqrt = true;
				} else if ("0123456789.".indexOf(text) >= 0) {
					// 用户按了数字键或者小数点键
					handleNumber(text);
					// handlezero(zero);
				} else {
					// 用户按了运算符键
					handleOperator(text);
				}

			}
		});
	}

	/**
	 * 处理数字键被按下的事件
	 *
	 * @param key
	 */
	private void handleNumber(String key) {
		if (firstKey) {
			// 输入的第一个数字
			if ((key.equals(".")) && (label2.getText().indexOf(".") < 0)) {
				label2.setText(label2.getText() + ".");
			} else {
				if (key.equals(".")) {
					label2.setText("0" + key);
				} else {
					label2.setText(key);
				}
			}
		} else if ((key.equals(".")) && (label2.getText().indexOf(".") < 0)) {
			// 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
			label2.setText(label2.getText() + ".");
		} else if (!key.equals(".")) {
			// 如果输入的不是小数点,则将数字附在结果文本框的后面
			label2.setText(label2.getText() + key);
		}
		// 以后输入的肯定不是第一个数字了
		firstKey = false;
	}

	String operator = "=";
	// 是否合法
	boolean operateValidFlag = true;
	// 计算中间 1+3-2
	private double resultNum = 0.0;
	private boolean issqrt = true;

	/**
	 * 处理运算符键被按下的事件
	 *
	 * @param key
	 */
	private void handleOperator(String key) {
		if (operator.equals("/")) {
			// 除法运算
			// 如果当前结果文本框中的值等于0
			if (getNumberFromText() == 0.0) {
				// 操作不合法
				operateValidFlag = false;
				label2.setText("除数不能为零");
			} else {
				resultNum /= getNumberFromText();
			}
		} else if (operator.equals("1/X")) {
			// 倒数运算
			if (resultNum == 0.0) {
				// 操作不合法
				operateValidFlag = false;
				label2.setText("零没有倒数");
			} else {
				resultNum = 1 / resultNum;
			}
		} else if (operator.equals("+")) {
			// 加法运算
			resultNum += getNumberFromText();
		} else if (operator.equals("-")) {
			// 减法运算
			resultNum -= getNumberFromText();
		} else if (operator.equals("*")) {
			// 乘法运算
			resultNum *= getNumberFromText();
		} else if (operator.equals("√")) {
			// 平方根运算
			if (resultNum < 0) {
				issqrt = false;
				label2.setText("无效输入");
				resultNum = 0;
			} else {
				resultNum = Math.sqrt(resultNum);
			}

		} else if (operator.equals("%")) {
			// 百分号运算,除以100
			resultNum = resultNum / 100;

		} else if (operator.equals("±")) {
			// 正数负数运算
			resultNum = resultNum * (-1);
		} else if (operator.equals("=")) {
			// 赋值运算
			resultNum = getNumberFromText();

		}
		if (operateValidFlag) {
			// 双精度浮点数的运算
			long t1;
			double t2;
			t1 = (long) resultNum;
			t2 = resultNum - t1;
			if (t2 == 0) {
				if (issqrt) {
					label2.setText(String.valueOf(t1));
				}
				if (key.equals("=")) {
					label.setText("0");
				} else {
					label.setText(label2.getText() + key);
				}
			} else {
				label2.setText(String.valueOf(resultNum));
				if (key.equals("=")) {
					label.setText("0");
				} else {
					label.setText(label2.getText() + key);
				}
			}
		}
		// 运算符等于用户按的按钮
		operator = key;
		firstKey = true;
		operateValidFlag = true;
	}

	/**
	 * 从结果文本框中获取数字
	 *
	 * @return
	 */
	private double getNumberFromText() {
		double result = 0;
		try {
			result = Double.valueOf(label2.getText()).doubleValue();
		} catch (NumberFormatException e) {
		}
		return result;
	}

	/**
	 * 特殊网格布局的配置方法,添加组件
	 * 
	 * @param gridx
	 *            列的位置
	 * @param gridy
	 *            行的位置
	 * @param gridwidth
	 *            所跨的列数
	 * @param gridheight
	 *            所跨的行数
	 * @param constraints
	 *            配置文件类的对象
	 * @param mainpanel
	 *            主面板对象
	 * @param panel
	 *            组件的对象
	 */
	public void addCommpot(int gridx, int gridy, int gridwidth, int gridheight, GridBagConstraints constraints,
			JPanel mainpanel, JPanel panel) {
		constraints.gridx = gridx;
		constraints.gridy = gridy;
		constraints.gridwidth = gridwidth;
		constraints.gridheight = gridheight;
		mainpanel.add(panel, constraints);
	}

	/**
	 * 特殊网格布局的配置方法,添加组件
	 * 
	 * @param gridx
	 *            列的位置
	 * @param gridy
	 *            行的位置
	 * @param gridwidth
	 *            所跨的列数
	 * @param gridheight
	 *            所跨的行数
	 * @param constraints
	 *            配置文件类的对象
	 * @param mainpanel
	 *            主面板对象
	 * @param button
	 *            组件的对象
	 */
	public void addCommpot(int gridx, int gridy, int gridwidth, int gridheight, GridBagConstraints constraints,
			JPanel mainpanel, JButton button) {
		constraints.gridx = gridx;
		constraints.gridy = gridy;
		constraints.gridwidth = gridwidth;
		constraints.gridheight = gridheight;
		mainpanel.add(button, constraints);
	}

	/**
	 * 设置窗口中心显示
	 * 
	 * @param frame
	 */
	public void center(JFrame frame) {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension dim = kit.getScreenSize();
		int x = (dim.width - frame_width) / 2;
		int y = (dim.height - frame_height) / 2;

		frame.setLocation(x, y);
	}

	/**
	 * 添加菜单
	 * 
	 * @param frame
	 */
	public void addMenu(JFrame frame) {
		JMenuBar bar = new JMenuBar();

		JMenu vMenu = new JMenu("查看(V)");
		JMenu eMenu = new JMenu("编辑(E)");
		JMenu hMenu = new JMenu("帮助(H)");

		vMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		eMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
		hMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));

		bar.add(vMenu);
		bar.add(eMenu);
		bar.add(hMenu);

		frame.setJMenuBar(bar);
	}

	public static void main(String[] args) {
		new Calculate();
	}

}

你可能感兴趣的:(Java,GUI,java,JavaSE,eclipse,intellij-idea)