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

📄 e6_1.java

📁 一个简单上网计算器程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class E6_1 extends Frame implements ActionListener
{
	private TextField text;
	private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,bs,bm,bc,bce,bd;
	private Button bp,be,bco,bj,bn,b0;
	private Panel p1,p2;
    private int number=1;
    private double plus_number,bplus_number,result=0;
    private char sign='n';
    private char record='y';
	public E6_1()
	{
		super("Calculate");
		this.setSize(360,250);
		this.setBackground(Color.LIGHT_GRAY);
		this.setLocation(300,240);
		
		text=new TextField("0",22);
		
		text.setEditable(false);
		text.setBackground(Color.white);
		this.setLayout(new BorderLayout());
		
		this.add(text,BorderLayout.NORTH);
		
		p2=new Panel(new GridLayout(5,4));
        p2.setSize(190, 200);		
        
		this.add(p2,BorderLayout.CENTER);
		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");
		b0=new Button("0");
		bp=new Button("+");
		bj=new Button("-");
		bm=new Button("*");
		bd= new Button("/");
		be=new Button("=");
		bc=new Button("C");
		bce=new Button("CE");
		bs=new Button("sqrt");
		bn=new Button("+/-");
		bco=new Button(".");
		p2.add(bs);
		p2.add(bn);
		p2.add(bce);
		p2.add(bc);	
		p2.add(b7);
		p2.add(b8);
		p2.add(b9);
		p2.add(bd);
		p2.add(b4);
		p2.add(b5);
		p2.add(b6);
		p2.add(bm);
		p2.add(b1);
		p2.add(b2);
		p2.add(b3);	
		p2.add(bj);
		p2.add(b0);
		p2.add(bco);
		p2.add(be);
	    p2.add(bp);
	
	    b1.addActionListener(this);
		b2.addActionListener(this);
		b0.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		b6.addActionListener(this);
		b7.addActionListener(this);
		b8.addActionListener(this);
		b9.addActionListener(this);
		bce.addActionListener(this);
		bc.addActionListener(this);
		bp.addActionListener(this);
		bj.addActionListener(this);
		bm.addActionListener(this);
		bs.addActionListener(this);
		be.addActionListener(this);
		bco.addActionListener(this);
		bn.addActionListener(this);
		bd.addActionListener(this);
		
		this.addWindowListener(new WinClose());
		
		this.setVisible(true);
		this.setResizable(false);
		
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==b0)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b1)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b2)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b3)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b4)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b5)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b6)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b7)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b8)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==b9)
		{
			if(record=='y')
			{
     			  if(text.getText().equals("0"))
	    			text.setText(e.getActionCommand());
	    		else text.setText(text.getText()+e.getActionCommand());
		    }
			else text.setText(e.getActionCommand());
			record='y';
		}
		if(e.getSource()==bco)
		{
			int i,j=1;
			for(i=0;i<text.getText().length();i++)
			{
				if(text.getText().charAt(i)=='.')
					j++;
			}
			if(j!=1)
				text.setText(text.getText());
			else
			text.setText(text.getText()+e.getActionCommand());
		}
		if(e.getSource()==bce)
		{
			text.setText("0");
			this.result=0;
		}
		if(e.getSource()==bc)
		{
			String s=text.getText();
			if(s.length()==1)
				text.setText("0");
			else {
				s=s.substring(0,s.length()-1);
				text.setText(s);
			}
		}
		if(e.getSource()==bs)
		{
		
			int zhengshu;
			zhengshu=(int)Math.sqrt(Double.parseDouble(text.getText()));
			if(zhengshu==Math.sqrt(Double.parseDouble(text.getText())))
		    	  text.setText(""+zhengshu);
			else text.setText(""+Math.sqrt(Double.parseDouble(text.getText())));
		}
		if(e.getSource()==bn)
		{
			if(Double.parseDouble(text.getText())>=0)
			{
				if(text.getText().equals("-0"))
					text.setText("0");
				else text.setText("-"+text.getText());
			}
			else text.setText(text.getText().substring(1,text.getText().length()));
			this.number++;
		}
		if(e.getSource()==bm)
		{
			int zhengshu;
			plus_number=Double.parseDouble(text.getText());
			sign='*';
			if(result==0){
				result=1;
				result*=plus_number;
			}
			else if(result==plus_number)
				result=result;
			else result*=plus_number;
			zhengshu=(int)result;
			if(zhengshu==result)
				text.setText(""+zhengshu);
			else text.setText(""+result);
			this.record='n';
		}
		if(e.getSource()==bp)
		{
			int zhengshu;
			plus_number=Double.parseDouble(text.getText());
			sign='+';
			if(result==plus_number)
				result=result;
			else result+=plus_number;
			zhengshu=(int)result;
			if(zhengshu==result)
				text.setText(""+zhengshu);
			else text.setText(""+result);
			this.record='n';
		}
		if(e.getSource()==bj)
		{
			int zhengshu;
			plus_number=Double.parseDouble(text.getText());
			sign='-';
			if(result==0)
			{
				result=plus_number;
			}
			else if(result==plus_number)
				result=result;
			else result-=plus_number;
			zhengshu=(int)result;
			if(zhengshu==result)
				text.setText(""+zhengshu);
			else text.setText(""+result);
			this.record='n';
		}
		if(e.getSource()==bd)
		{
			int zhengshu;
			plus_number=Double.parseDouble(text.getText());
			sign='/';
			if(result==0)
			{
				result=plus_number;
			}
			else if(result==plus_number)
				result=result;
			else result/=plus_number;
			zhengshu=(int)result;
			if(zhengshu==result)
				text.setText(""+zhengshu);
			else text.setText(""+result);
			this.record='n';
		}
		if(e.getSource()==be)
		{
			int zhengshu;
			bplus_number=Double.parseDouble(text.getText());
			if(sign=='+')
				result+=bplus_number;
			if(sign=='-')
				result-=bplus_number;
			if(sign=='*')
				result*=bplus_number;
			if(sign=='/')
				result/=bplus_number;
			if(sign=='n')
				result=Double.parseDouble(text.getText());
			zhengshu=(int)result;
			if(zhengshu==result)
				text.setText(""+zhengshu);
			else text.setText(""+result);
			this.record='n';
		}
	}
	public static void main(String[] args)
	{
		new E6_1();
	}
}
class WinClose  extends WindowAdapter
{
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}

⌨️ 快捷键说明

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