⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calcl.java

📁 是一个实现加减乘除的java小程序
💻 JAVA
字号:
package practice;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calcl extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7319327513849853930L;

	JFrame f;

	private JButton jia = new JButton("   +");

	private JButton jian = new JButton("   -  ");

	private JButton cheng = new JButton("   *  ");

	private JButton chu = new JButton("   /  ");

	private JButton deng = new JButton("       =    ");

	private JButton qiuyu = new JButton("%");

	private JButton fu = new JButton("+/-");

	private JButton dian = new JButton(".");

	private JButton kai = new JButton(" sqrt ");

	TextField text;

	JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;

	JButton backspace, ce, c;

	double qian;

	String fuhao;

	boolean clickable = true, clear = true;

	int first = 1;

	public void display() {
		f = new JFrame("计算器—樊永生制作");

		f.setSize(210, 260);
		f.setLocation(320, 240);
		f.setBackground(new Color(198, 198, 198));
		f.setLayout(new FlowLayout(FlowLayout.LEFT));

		text = new TextField(40);
		text.setBackground(new Color(255, 255, 255));
		text.setEditable(true);
		f.add(text);

		b0 = new JButton("0");
		b1 = new JButton("1");
		b2 = new JButton("2");
		b3 = new JButton("3");
		b4 = new JButton("4");
		b5 = new JButton("5");
		b6 = new JButton("6");
		b7 = new JButton("7");
		b8 = new JButton("8");
		b9 = new JButton("9");
		b10 = new JButton("C");
		b11 = new JButton("CE");

		backspace = new JButton("Baseback");
		backspace.setForeground(new Color(255, 0, 0));
		backspace.addActionListener(this);

		f.add(backspace);
		f.add(b11);
		f.add(b10);
		f.add(b7);
		f.add(b8);
		f.add(b9);
		f.add(chu);
		chu.setForeground(new Color(255, 0, 0));
		f.add(b4);
		f.add(b5);
		f.add(b6);
		f.add(cheng);
		cheng.setForeground(new Color(255, 0, 0));
		f.add(b1);
		f.add(b2);
		f.add(b3);
		f.add(jian);
		jian.setForeground(new Color(255, 0, 0));
		f.add(b0);
		f.add(dian);
		f.add(fu);
		f.add(jia);
		jia.setForeground(new Color(255, 0, 0));
		f.add(qiuyu);
		f.add(kai);
		kai.setForeground(new Color(255, 0, 0));
		f.add(deng);
		deng.setForeground(new Color(255, 0, 0));
		chu.setForeground(new Color(255, 0, 0));

		kai.addActionListener(this);
		chu.addActionListener(this);
		b0.addActionListener(this); //为按钮b0注册事件监听程序
		b1.addActionListener(this); //为按钮b1注册事件监听程序
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		b6.addActionListener(this);
		b7.addActionListener(this);
		b8.addActionListener(this);
		b9.addActionListener(this);
		b10.addActionListener(this);
		jia.addActionListener(this);
		jian.addActionListener(this);
		cheng.addActionListener(this);
		deng.addActionListener(this);
		dian.addActionListener(this);
		chu.addActionListener(this);
		b11.addActionListener(this);
		qiuyu.addActionListener(this);
		fu.addActionListener(this);

		f.addWindowListener(new WinClose());//为f注册监听程序
		f.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		Object temp = e.getSource();

		if (first == 1)
			text.setText("");
		first = 0;//第一次把文本框0.清空

		if (temp == backspace) {//退格
			String s = text.getText();
			text.setText("");
			for (int i = 0; i < s.length() - 1; i++) {
				char a = s.charAt(i);
				text.setText(text.getText() + a);
			}
		}
		if (temp == b11) {
			text.setText("0.");
			clear = true;
			first = 1;
		}
		if (temp == b10) {

			text.setText("0.");
			clear = true;
			first = 1;
		}

		if (temp == b0) {
			if (clear == false)//判断是否点击了符号位
				text.setText("");
			text.setText(text.getText() + "0");
		}

		if (temp == b1) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "1");
			clear = true;//第二次不在清空(前二句)
		}

		if (temp == b2) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "2");
			clear = true;
		}

		if (temp == b3) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "3");
			clear = true;
		}

		if (temp == b4) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "4");
			clear = true;
		}

		if (temp == b5) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "5");
			clear = true;
		}

		if (temp == b6) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "6");
			clear = true;
		}
		if (temp == b7) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "7");
			clear = true;
		}

		if (temp == b8) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "8");
			clear = true;
		}

		if (temp == b9) {
			if (clear == false)
				text.setText("");
			text.setText(text.getText() + "9");
			clear = true;
		}
		if (temp == dian) {
			clickable = true;
			for (int i = 0; i < text.getText().length(); i++)
				if ('.' == text.getText().charAt(i)) {
					clickable = false;
					break;
				} //第一层判断是否里面含有小数点;

			if (clickable == true)//第二次判断
				text.setText(text.getText() + ".");

		}
		try {
			if (temp == jia) {// 加法
				qian = Double.parseDouble(text.getText());
				fuhao = "+";
				clear = false;// 要清空前一次的数据
			}
			if (temp == jian) {
				qian = Double.parseDouble(text.getText());
				fuhao = "-";
				clear = false;// 要清空前一次的数据

			}
			if (temp == cheng) {
				qian = Double.parseDouble(text.getText());
				fuhao = "*";
				clear = false;// 要清空前一次的数据

			}
			if (temp == chu) {
				qian = Double.parseDouble(text.getText());
				fuhao = "/";
				clear = false;// 要清空前一次的数据

			}
			if (temp == deng) {
				double ss = Double.parseDouble(text.getText());
				text.setText("");
				if (fuhao == "+")
					text.setText(qian + ss + "");
				if (fuhao == "-")
					text.setText(qian - ss + "");
				if (fuhao == "*")
					text.setText(qian * ss + "");
				if (fuhao == "/")
					text.setText(qian / ss + "");
				clear = false;// 要清空前一次的数据

			}

			if (temp == kai) {
				String s = text.getText();
				if (s.charAt(0) == '-') {
					text.setText("负数不能开根号");
				} else
					text.setText(Double.toString(java.lang.Math.sqrt(Double
							.parseDouble(text.getText()))));
				clear = false;
			}
			if (temp == qiuyu) {
				text.setText("0");
				clear = false;
			}
			if (temp == fu) {
				boolean isNumber = true;
				String s = text.getText();
				for (int i = 0; i < s.length(); i++)
					if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9'
							|| s.charAt(i) == '.' || s.charAt(i) == '-')) {
						isNumber = false;
						break;
					}
				if (isNumber == true) {
					//如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉 
					if (s.charAt(0) == '-') {
						text.setText("");
						for (int i = 1; i < s.length(); i++) {
							char a = s.charAt(i);
							text.setText(text.getText() + a);
						}
					}
					//如果当前字符串第一个字符不是符号,则添加一个符号在首字母处 
					else
						text.setText('-' + s);
				}
			}

		} catch (Exception eee) {
			System.out.println("运算时,首先输入数字或字符");
			text.setText("运算出错");
			clear = false;
		}

	}

	public static void main(String[] args) {
		(new Calcl()).display();
	}
}

class WinClose extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -