java 简易计算器代码

demo1:

import java.awt.*;
import java.awt.event.*; 
public class num  implements ActionListener
{ 
	Frame win=new Frame("jiandanjisuanqi");
	Panel num=new Panel();
	Panel meth = new Panel();
	TextArea display=new TextArea();
	Button add1,sub,mul,div; 
	int no1,no2; 
	float no3; 
	String ch,ch3;
	Button res=new Button("=");
	Button canc=new Button("CE");
	Button but7=new Button("7");
	Button but8=new Button("8");
	Button but9=new Button("9");
    Button but4=new Button("4");
	Button but5=new Button("5");
	Button but6=new Button("6");
	Button but1=new Button("1");
	Button but2=new Button("2");
	Button but3=new Button("3");
	Button but0=new Button("0");
	public void test() 
	{ 
		add1=new Button("+"); 
		sub=new Button("-"); 
		mul=new Button("*"); 
		div=new Button("/");
		win.setLayout(new GridLayout(4,0));
		num.setLayout(new GridLayout(4,4));
		num.add(but7);
		num.add(but8); 
		num.add(but9);
		num.add(add1); 
		num.add(but4);
		num.add(but5);
		num.add(but6);
		num.add(sub);
        num.add(but1); 
		num.add(but2); 
		num.add(but3);
		num.add(mul); 
		num.add(but0); 
		num.add(canc); 
		num.add(res);
		num.add(div); 
		but0.addActionListener(this);
		but1.addActionListener(this);
		but2.addActionListener(this);
		but3.addActionListener(this);
		but4.addActionListener(this);
		but5.addActionListener(this);
		but6.addActionListener(this);
		but7.addActionListener(this);
		but8.addActionListener(this);
		but9.addActionListener(this);
		canc.addActionListener(new CE()); 
		add1.addActionListener(new METH());
		sub.addActionListener(new METH());
		mul.addActionListener(new METH());
		div.addActionListener(new METH()); 
		res.addActionListener(new RES()); 
		win.add(display); 
		win.add(num); 
		win.setSize(400,400);
		//win.setBackground(Color.black);
		win.setForeground(Color.red);
		win.setVisible(true); 
		win.addWindowListener(new WindowAdapter()
		{ 
			public void windowClosing(WindowEvent e)
			{ 
				System.exit(0); 
			} 
			}
		); 
		} 
	public void actionPerformed(ActionEvent e)
	{ 
		display.append(e.getActionCommand()); 
	} 
	private class CE implements ActionListener
	{ 
		public void actionPerformed(ActionEvent ex)
		{ 
			display.setText("");
			no1=0;
			no2=0;
			no3=0;
		}
	} 
	private class METH implements ActionListener
	{ 
		public void actionPerformed(ActionEvent ey)
		{ 
			no1=Integer.parseInt(display.getText()); 
			display.setText("");
			ch=ey.getActionCommand();
		} 
	}
	private class RES implements ActionListener
	{ 
		public void actionPerformed(ActionEvent es)
		{ 
			no2=Integer.parseInt(display.getText());
			if(ch=="+") 
			{
				no3=no1+no2;
				ch3=Float.toString(no3);
				display.setText(ch3); 
			} 
			else if(ch=="-") 
			{
				no3=no1-no2; 
				ch3=Float.toString(no3);
				display.setText(ch3);
			} 
			else if(ch=="*") 
			{
				no3=no1*no2; 
				ch3=Float.toString(no3);
				display.setText(ch3);
			} 
			else if(ch=="/") 
			{
					no3=(float)no1/no2; 
					ch3=Float.toString(no3);
					display.setText(ch3); 
					if(no2==0){
						display.setText("");
					}
			} 
		} 
	} 
	public static void main (String[]args)
	{ 
		num s=new num();
		s.test(); 
	} 
}







/*public class num{
	public static void main(String[] arge){
		System.out.print(switchIt(2));
		
	}
	public static int switchIt(int x){
		int j=1;
		switch (x) {
		case 1:j++;
		case 2:j++;
		case 3:j++;

		default: j++;
			break;
		}
		return j+x;
	}
}*/

demo2;

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

public class Calculator extends WindowAdapter implements ActionListener {
	JFrame list;
	JTextField show;
	JButton bc, c, ce, ab, jia, jian, cheng, chu, equ, point, sqrt, ds, bfh, zf;
	// 按钮 退格,清空,复位,关于,加,减,乘,除,等号,小数点,2次方根,倒数,百分号,正负号
	JButton b[] = new JButton[10]; // 按钮数组,数字键0~9
	double sum = 0, getValue;
	int i = 0, j = 0, p = 0, l, action;
	JDialog about;
	final int slength = 30; // 设置结果显示有效长度

	public void disp() {
		list = new JFrame("简易计算器");
		list.setSize(360, 230);
		list.setLocation(380, 260);
		list.setBackground(Color.LIGHT_GRAY);
		list.setLayout(new FlowLayout(FlowLayout.CENTER));
		list.setResizable(false);
		show = new JTextField(31);
		show.setText("0");
		show.setHorizontalAlignment(SwingConstants.RIGHT);
		show.setEditable(false);
		list.add(show);

		Panel dispTop = new Panel();
		list.add(dispTop);
		dispTop.setLayout(new GridLayout(1, 4, 3, 3));
		bc = new JButton("  Back    ");
		bc.setForeground(Color.BLUE);
		dispTop.add(bc);
		ce = new JButton("  CE   ");
		ce.setForeground(Color.BLUE);
		dispTop.add(ce);
		c = new JButton("  C   ");
		c.setForeground(Color.BLUE);
		dispTop.add(c);
		ab = new JButton("   About   ");
		ab.setForeground(Color.BLUE);
		dispTop.add(ab);
		about = new JDialog(list, "关于计算器", true);
		Label ct = new Label("hello world!", 1);
		ct.setForeground(Color.RED);
		about.add(ct, "Center");
		about.setSize(200, 100);
		about.setLocation(500, 300);
		about.addWindowListener(this);

		Panel dispMain = new Panel();
		list.add(dispMain);
		dispMain.setLayout(new GridLayout(1, 2, 10, 10));

		Panel dispLeft = new Panel();
		dispMain.add(dispLeft);
		dispLeft.setLayout(new GridLayout(4, 3, 3, 3));

		Panel dispRight = new Panel();
		dispMain.add(dispRight);
		dispRight.setLayout(new GridLayout(4, 2, 3, 3));
		for (l = 9; l >= 0; l--) {
			b[l] = new JButton(String.valueOf(l));
			dispLeft.add(b[l]);
			b[l].addActionListener(this);
		}
		jia = new JButton("+");
		jia.setForeground(Color.RED);
		jian = new JButton("-");
		jian.setForeground(Color.RED);
		cheng = new JButton("*");
		cheng.setForeground(Color.RED);
		chu = new JButton("/");
		chu.setForeground(Color.RED);
		equ = new JButton("=");
		equ.setForeground(Color.RED);
		point = new JButton(".");
		zf = new JButton(" +/- ");
		sqrt = new JButton("sqrt");
		bfh = new JButton("%");
		ds = new JButton("1/x");
		dispRight.add(chu);
		dispRight.add(sqrt);
		dispRight.add(cheng);
		dispRight.add(bfh);
		dispRight.add(jian);
		dispRight.add(ds);
		dispRight.add(jia);
		dispRight.add(equ);
		dispLeft.add(zf);
		dispLeft.add(point);
		bc.addActionListener(this);
		ce.addActionListener(this);
		c.addActionListener(this);
		ab.addActionListener(this);
		jia.addActionListener(this);
		jian.addActionListener(this);
		cheng.addActionListener(this);
		chu.addActionListener(this);
		equ.addActionListener(this);
		point.addActionListener(this);
		zf.addActionListener(this);
		sqrt.addActionListener(this);
		bfh.addActionListener(this);
		ds.addActionListener(this);
		list.addWindowListener(this);
		list.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		getValue = Double.valueOf(show.getText()).doubleValue();
		// getValue=Double.valueOf(show.getText());//如果该行出错,则在后面加上.doubleValue();属版本兼容问题
		if (e.getSource() == jia) { // 加运算,可连加
			if (j == 0) {
				sum = getValue;
			} else if (action == 1) {
				sum += getValue;
			}
			setSum();
			j++;
			p = 0;
			i = 0;
			action = 1;
		} else if (e.getSource() == jian) { // 减运算,可连减
			if (j == 0) {
				sum = getValue;
			} else if (action == 2) {
				sum -= getValue;
			}
			setSum();
			j++;
			p = 0;
			i = 0;
			action = 2;
		} else if (e.getSource() == cheng) { // 乘运算,可连乘
			if (j == 0) {
				sum = getValue;
			} else if (action == 3) {
				sum *= getValue;
			}
			setSum();
			j++;
			p = 0;
			i = 0;
			action = 3;
		} else if (e.getSource() == chu) { // 除运算,可连除
			if (j == 0)
				sum = getValue;
			else if (action == 4) {
				sum /= getValue;
			}
			setSum();
			j++;
			p = 0;
			i = 0;
			action = 4;
		} else if (e.getSource() == equ) { // 等号,运算最后一个操作数
			switch (action) {
			case 1:
				show.setText(String.valueOf(sum += getValue));
				break;
			case 2:
				show.setText(String.valueOf(sum -= getValue));
				break;
			case 3:
				show.setText(String.valueOf(sum *= getValue));
				break;
			case 4:
				show.setText(String.valueOf(sum /= getValue));
				break;
			}
			setSum();
			i = 0;
			j = 0;
			action = 0;
		} else if (e.getSource() == point) { // 小数点,只能按一个小数点
			if (p == 0)
				show.setText(show.getText() + e.getActionCommand());
			p = 1;
		} else if (e.getSource() == c || e.getSource() == ce) { // 清空与复位
			i = 0;
			j = 0;
			p = 0;
			sum = 0;
			action = 0;
			show.setText("0");
		} else if (e.getSource() == bc) { // 退格
			String s = show.getText();
			if (s.length() > 1) {
				show.setText("");
				for (l = 0; l < s.length() - 1; l++) { // 按一下,删除尾部一位
					char a = s.charAt(l);
					show.setText(show.getText() + a);
				}
			} else
				show.setText("0");
		} else if (e.getSource() == ab) { // 关于
			about.setVisible(true);
		} else if (e.getSource() == sqrt) { // 开2次方根
			sum = Math.sqrt(getValue);
			setSum();
			j++;
		} else if (e.getSource() == ds) { // 求倒数
			sum = 1 / getValue;
			setSum();
			j++;
		} else if (e.getSource() == bfh) { // 百分号
			sum = getValue / 100;
			setSum();
			j++;
		} else if (e.getSource() == zf) { // 正负号切换,正号不显示
			String s = show.getText();
			char a = s.charAt(0);
			if (a == '-') {
				show.setText("");
				for (l = 1; l < s.length(); l++) { // 去掉负号
					show.setText(show.getText() + s.charAt(l));
				}
			} else if (getValue != 0) { // 加上负号
				show.setText("-" + s);
			}
		}
		for (l = 0; l < 10; l++) { // 0~9数字键触发
			if (e.getSource() == b[l]) {
				if (i == 0)
					show.setText("");
				String s = show.getText();
				if (s.length() < slength)
					show.setText(show.getText() + e.getActionCommand());
				if (e.getSource() == b[0] && getValue == 0 && p == 0)
					show.setText("0");
				if (e.getSource() != b[0] && getValue == 0 && p == 0)
					show.setText(e.getActionCommand());
				i++;
			}
		}
	}

	public void setSum() { // 把计算结果显示出来
		show.setText(String.valueOf(sum));
		String s = show.getText();
		char a = s.charAt((s.length() - 1));
		char b = s.charAt((s.length() - 2));
		if (a == '0' && b == '.') { // 如果是整数,则去掉后面的小数点和0
			show.setText(String.valueOf(Math.round(sum)));
		}
	}

	public void windowClosing(WindowEvent e) {
		if (e.getSource() == about)
			about.setVisible(false);
		else if (e.getSource() == list)
			System.exit(0);
	}

	public static void main(String args[]) {
		(new Calculator()).disp();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值