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

📄 counterapp.java

📁 Java 开发的一个计算器程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class CounterApp extends Frame
{
	Panel p;
	Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
	Button add,sub,mul,div;
	Button equ,clear;
	Button bh,dot;
	Button sqrt,cf,exp;
	Button sin,cos,tan;
	TextField t;
    String snum,enum;
    String oper;
    double result;
    Font f;

	CounterApp()
	{
		p=new Panel();
		t=new TextField();
		b0=new Button("0");
		b1=new Button("1");
		b2=new Button("2");
		b3=new Button("3");
		b4=new Button("4");
		b5=new Button("5");
		b6=new Button("6");
		b7=new Button("7");
		b8=new Button("8");
		b9=new Button("9");
		add=new Button("+");
		sub=new Button("-");
		mul=new Button("*");
		div=new Button("/");
		equ=new Button("=");
		bh=new Button("+/-");
		dot=new Button(".");
		sqrt=new Button("Sqrt");
		cf=new Button("y(x)");
		exp=new Button("Exp");
		sin=new Button("Sin");
		cos=new Button("Cos");
		tan=new Button("Tan");
		String font[]=Toolkit.getDefaultToolkit().getFontList();
		t.setText(null);
		t.setBackground(Color.black);
		t.setForeground(Color.red);
		f=new  Font(font[(int)Math.random()*font.length],Font.BOLD,20);
		t.setFont(f);
		clear=new Button("Clear");
		p.setLayout(new GridLayout(4,6,2,2));
		b0.setForeground(Color.red);
		b1.setForeground(Color.red);
		b2.setForeground(Color.red);
		b3.setForeground(Color.red);
		b4.setForeground(Color.red);
		b5.setForeground(Color.red);
		b6.setForeground(Color.red);
		b7.setForeground(Color.red);
		b8.setForeground(Color.red);
		b9.setForeground(Color.red);
		add.setForeground(Color.BLUE);
		sub.setForeground(Color.blue);
		mul.setForeground(Color.BLUE);
		div.setForeground(Color.BLUE);
		bh.setForeground(Color.BLUE);
		dot.setForeground(Color.BLUE);
		exp.setForeground(Color.BLUE);
		cf.setForeground(Color.BLUE);
		sqrt.setForeground(Color.BLUE);
		sin.setForeground(Color.BLUE);
		cos.setForeground(Color.BLUE);
		tan.setForeground(Color.BLUE);
		add.setFont(f);
		sub.setFont(f);
		mul.setFont(f);
		div.setFont(f);
		equ.setFont(f);
		dot.setFont(f);
		bh.setFont(f);
		equ.setForeground(Color.blue);
		clear.setForeground(Color.blue);
		p.add(b0);
		p.add(b1);
		p.add(b2);
		p.add(add);
		p.add(exp);
		p.add(sin);
		p.add(b3);
		p.add(b4);
		p.add(b5);
		p.add(sub);
		p.add(cf);
		p.add(cos);
		p.add(b6);
		p.add(b7);
		p.add(b8);
		p.add(mul);
		p.add(sqrt);
		p.add(tan);
		p.add(b9);
		p.add(bh);
		p.add(dot);
		p.add(div);
		p.add(equ);
		p.add(clear);
		this.setLayout(new BorderLayout());
		this.add("North",t);
		this.add("Center",p);
		this.resize(300,150);
		
		b0.addActionListener(new ButtonAction());
		b1.addActionListener(new ButtonAction());
		b2.addActionListener(new ButtonAction());
		b3.addActionListener(new ButtonAction());
		b4.addActionListener(new ButtonAction());
		b5.addActionListener(new ButtonAction());
		b6.addActionListener(new ButtonAction());
		b7.addActionListener(new ButtonAction());
		b8.addActionListener(new ButtonAction());
		b9.addActionListener(new ButtonAction());
		add.addActionListener(new ButtonAction());
		sub.addActionListener(new ButtonAction());
		mul.addActionListener(new ButtonAction());
		div.addActionListener(new ButtonAction());
		equ.addActionListener(new ButtonAction());
		clear.addActionListener(new ButtonAction());
		dot.addActionListener(new ButtonAction());
		bh.addActionListener(new ButtonAction());
		this.addWindowListener(new WinClose());
		exp.addActionListener(new ButtonAction());
		sqrt.addActionListener(new ButtonAction());
		cf.addActionListener(new ButtonAction());
		sin.addActionListener(new ButtonAction());
		cos.addActionListener(new ButtonAction());
		tan.addActionListener(new ButtonAction());
	
			}
	public static void main(String[] args)
	{
	CounterApp counter=new CounterApp();
		counter.show();
		}
	class ButtonAction implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{  String st=t.getText();
			if(e.getSource().equals(b0))
			t.setText(st+b0.getLabel());
			  if(e.getSource().equals(b1))
			 t.setText(st+b1.getLabel());
			 if(e.getSource().equals(b2))
			 t.setText(st+b2.getLabel());
			 if(e.getSource().equals(b3))
			 t.setText(st+b3.getLabel());
			 if(e.getSource().equals(b4))
			 t.setText(st+b4.getLabel());
			 if(e.getSource().equals(b5))
			 t.setText(st+b5.getLabel());
			 if(e.getSource().equals(b6))
			 t.setText(st+b6.getLabel());
			 if(e.getSource().equals(b7))
			 t.setText(st+b7.getLabel());
			 if(e.getSource().equals(b8))
			 t.setText(st+b8.getLabel());
			 if(e.getSource().equals(b9))
			 t.setText(st+b9.getLabel());
			 if(e.getSource().equals(dot))
			 t.setText(st+dot.getLabel());
			 if(e.getSource().equals(add))
			  {
			  	snum=t.getText();
			  	t.setText(null);
			  	oper=add.getLabel();
			  	}
			  	if(e.getSource().equals(sub))
			  {
			  	snum=t.getText();
			  	t.setText(null);
			  	oper=sub.getLabel();
			  	}
			  	if(e.getSource().equals(mul))
			  {
			  	snum=t.getText();
			  	t.setText(null);
			  	oper=mul.getLabel();
			  	}
			  	if(e.getSource().equals(div))
			  {
			  	snum=t.getText();
			  	t.setText(null);
			  	oper=div.getLabel();
			  	}
			  	if(e.getSource().equals(cf))
			  	{	snum=t.getText();
			  		t.setText(null);
			  		oper="^";
			  		}
			  	if(e.getSource().equals(equ))
			  	{
			  		enum=t.getText();
			  		if(oper=="+")
			  		result=Double.parseDouble(snum)+Double.parseDouble(enum);
			  		if(oper=="-")
			  		result=Double.parseDouble(snum)-Double.parseDouble(enum);
			  		if(oper=="*")
			  		result=Double.parseDouble(snum)*Double.parseDouble(enum);
			  		if(oper=="/")
			  		{result=Double.parseDouble(snum)/Double.parseDouble(enum);
			  		result=(double)new Double(result).floatValue();
			  		}
			  		if(oper=="^")
			  		result=Math.pow(Double.parseDouble(snum),Double.parseDouble(enum));
			  		t.setText(Float.toString(new Double(result).floatValue()));
			  			}
			  	if(e.getSource().equals(clear))
			  	t.setText(null);
			if(e.getSource().equals(bh))
			{   snum=t.getText();
				if(st.charAt(0)=='-')
			t.setText(Double.toString(Math.abs(Double.parseDouble(snum))));
				else
				t.setText("-"+snum);
				} 
			if(e.getSource().equals(exp))
			  	{	snum=t.getText();
			  		result=Math.exp(Double.parseDouble(snum));
			  	    t.setText(Float.toString(new Double(result).floatValue()));
			  		}
			  
			  	if(e.getSource().equals(sqrt))
			  	{	snum=t.getText();
			  		result=Math.sqrt(Double.parseDouble(snum));
			  	    t.setText(Float.toString(new Double(result).floatValue()));
			  		}
			  	if(e.getSource().equals(sin))
			  	{	snum=t.getText();
			  	
			  		result=Math.sin(Double.parseDouble(snum)*(Math.PI/180));
			  	  t.setText(Float.toString(new Double(result).floatValue()));
			  	  }
			  	if(e.getSource().equals(cos))
			  	{	snum=t.getText();
			  		result=Math.cos(Double.parseDouble(snum)*(Math.PI/180));
			  	 t.setText(Float.toString(new Double(result).floatValue()));	
			  	}
			  		if(e.getSource().equals(tan))
			  	{	snum=t.getText();
			  		result=Math.tan(Double.parseDouble(snum)*(Math.PI/180));
			  	 t.setText(Float.toString(new Double(result).floatValue()));	
			  		}
			 }}	
			class WinClose extends WindowAdapter
			{
				public void windowClosing(WindowEvent e)
				{
					hide();
					dispose();
					System.exit(0);
					}}}

⌨️ 快捷键说明

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