首页>代码>JAVA Swing开发简易的计算器程序,java初学swing编写>/SimpleCalculator/src/hyw/calculator/Calculator.java
package hyw.calculator;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {

	final String JButtonName[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "×", ".", "0", "=", "÷" };
	Font font = new Font("宋体", Font.PLAIN, 17);
	JPanel p1, p2;
	JButton Jb[] = new JButton[JButtonName.length];// 创建按钮数组
	JButton ce;
	JTextField tf;

	public Calculator() {
		for (int i = 0; i < JButtonName.length; i++) {
			Jb[i] = new JButton("" + JButtonName[i]);//实例化并声明按钮名称
			Jb[i].setFont(font);// 设置字体
			Jb[i].setContentAreaFilled(false);// 设置按钮透明
			Jb[i].addActionListener(this);
		}
		p1 = new JPanel(new GridLayout(4, 4));// 设置成4X4的布 局
		for (int i = 0; i < JButtonName.length; i++) {
			p1.add(Jb[i]);
		}

		p2 = new JPanel(new BorderLayout());
		ce = new JButton("CE");
		ce.setContentAreaFilled(false);
		ce.addActionListener(this);
		tf = new JTextField("0");
		tf.setFont(font);
		p2.add(ce, "East");
		p2.add(tf, "Center");

		this.add(p2, BorderLayout.NORTH);
		this.add(p1, BorderLayout.CENTER);
		this.setVisible(true);
		this.setTitle("简易计算器");
		
		//使窗口居中
		int windowsWedth = 280;
		int windowsHeight = 300;
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.setBounds((width - windowsWedth) / 2,
				(height - windowsHeight) / 2, windowsWedth, windowsHeight);

	}

	public void actionPerformed(ActionEvent e) {

		String label = e.getActionCommand();// 返回的是按的按钮名
		if (e.getSource() == ce)
			Reset();
		else if ("0123456789.".indexOf(label) >= 0)// 查找按钮是否为数字和"."
			RunNumber(label);
		else
			RunOperator(label);

	}
	// 是否是第一次按数字

	boolean isFirstDigit = true;

	public void RunNumber(String key) {
		if (isFirstDigit)
			tf.setText(key);// 如果是第一次按数字,覆盖文本框的内容
		else if ((key.equals(".")) && (tf.getText().indexOf(".") < 0))// 按了点并且文本框中之前没有点
			tf.setText(tf.getText() + ".");//如果按了多个".",只显示一个
		else if (!key.equals("."))
			tf.setText(tf.getText() + key);// 叠加显示在文本框中
		isFirstDigit = false;
	}

	double number = 0.0;
	String operator = "=";

	public void Reset() {// 重置
		tf.setText("0");
		isFirstDigit = true;
		operator = "=";
	}

	public void RunOperator(String key) {
		if (operator.equals("+"))
			number += Double.valueOf(tf.getText());// 将文本框中的字符串强制转化为Double类型
		else if (operator.equals("-"))
			number -= Double.valueOf(tf.getText());
		else if (operator.equals("×"))
			number *= Double.valueOf(tf.getText());
		else if (operator.equals("÷")) {
			number /= Double.valueOf(tf.getText());
		} else if (operator.equals("="))
			number = Double.valueOf(tf.getText());
		tf.setText(String.valueOf(number));
		operator = key;
		isFirstDigit = true;
	}

	public static void main(String[] args) {
		new Calculator();
	}
}
最近下载更多
gdgdghd  LV1 2023年9月28日
微信网友_6533174388953088  LV1 2023年6月24日
ja12121  LV2 2023年6月17日
fss随便取的  LV1 2023年6月16日
另类清晨  LV8 2023年6月7日
Blithe1  LV1 2023年6月2日
2694809556  LV2 2022年12月22日
zhxlll  LV1 2022年12月21日
xixiwoaini  LV3 2022年12月17日
3272047233  LV1 2022年12月14日
最近浏览更多
22344341  LV2 4月21日
guyutian  LV2 4月21日
晨爽明宇  LV1 3月4日
1351408674  LV1 2023年12月27日
lshlsh 2023年12月25日
暂无贡献等级
myymyoyo  LV1 2023年12月17日
pangzhihui  LV12 2023年12月14日
gk_xmj  LV1 2023年12月6日
fesfefe  LV13 2023年11月25日
zzz963  LV1 2023年10月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友