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

📄 mycalculator.java

📁 我的一个java 计算器
💻 JAVA
字号:
package MyCalculator;
import java.awt.*; 
import java.awt.event.*; 
public  class MyCalculator{ 
 
String s,Str; 
static double a=0;
Panel	p1 = new Panel(); //新建两个面板
Panel	p2 = new Panel(); 
Button	b0 = new Button("0"); //定义显示的界面
Button	b1 = new Button("1"); 
Button	b2 = new Button("2"); 
Button	b3 = new Button("3"); 
Button	b4 = new Button("4"); 
Button	b5 = new Button("5"); 
Button	b6 = new Button("6"); 
Button	b7 = new Button("7"); 
Button	b8 = new Button("8"); 
Button	b9 = new Button("9"); 
Button	bPoint = new Button("."); //小数点
Button	bAdd = new Button("+"); 
Button	bDec = new Button("-"); 
Button	bMul = new Button("*"); 
Button	bDiv = new Button("/"); 
Button	bsurplus=new Button("%");
Button	bCal = new Button("=");
Button	bclean=new Button("Clean"); 
Button	bguo=new Button("作者");
Button	bs=new Button("说明");	
TextField tf= new TextField(25); //新建文本框显示输入输出结果

//-------------------------------------------------------------面板方法
public void launchFrame()
{   
    Frame f;
	f = new Frame(" MY Calculator"); 
	f.setBackground(Color.green);//设置背景为绿色
	f.setForeground(Color.blue);//设置前景为蓝色
    f.setSize(220,200); //上面面板宽为220.高为200
    f.setLocation(400,250);//运行后的位置
    f.setResizable(false); //不可动
    f.addWindowListener(new myWindowListener());
    tf.setEditable(false); //不可编辑,即不可以人为破坏输入或输出数据
p1.setLayout(new FlowLayout(FlowLayout.CENTER)); //流式布局居中
p1.add(tf);                    //将文本框加入到面板1中,让其显示
f.add(p1,BorderLayout.NORTH); //用方位布局把面板p1放在北面,即上方
p2.setLayout(new GridLayout(5,4)); //网格布局,5行4列
//监听事件,包括+,-,/,*,%,0..9等
b0.addActionListener(new guo_ActionListener()); //0
b1.addActionListener(new guo_ActionListener()); //1
b2.addActionListener(new guo_ActionListener()); //2
b3.addActionListener(new guo_ActionListener()); //3
b4.addActionListener(new guo_ActionListener()); 
b5.addActionListener(new guo_ActionListener()); 
b6.addActionListener(new guo_ActionListener()); 
b7.addActionListener(new guo_ActionListener()); 
b8.addActionListener(new guo_ActionListener()); 
b9.addActionListener(new guo_ActionListener()); 
bPoint.addActionListener(new guo_ActionListener()); //小数点
bAdd.addActionListener(new guo_ActionListener()); //加
bDec.addActionListener(new guo_ActionListener()); //减
bMul.addActionListener(new guo_ActionListener()); //乘
bDiv.addActionListener(new guo_ActionListener()); //除
bsurplus.addActionListener(new guo_ActionListener());//余
bCal.addActionListener(new guo_ActionListener()); // 等
bclean.addActionListener(new guo_ActionListener());//清除
bguo.addActionListener(new guo_ActionListener());//作者
bs.addActionListener(new guo_ActionListener());//说明

p2.add(b7); //按照789+  456-   123*   0.%/  clean= 作者,说明,     排窗口
p2.add(b8); 
p2.add(b9); 
p2.add(bAdd); 

p2.add(b4); 
p2.add(b5); 
p2.add(b6); 
p2.add(bDec); 

p2.add(b1); 
p2.add(b2); 
p2.add(b3); 
p2.add(bMul);
 
p2.add(b0); 
p2.add(bPoint); 
p2.add(bsurplus); 
p2.add(bDiv);

p2.add(bclean);
p2.add(bCal);
p2.add(bguo);
p2.add(bs);

f.add(p2,BorderLayout.SOUTH); //方位布局面板p2放在南面
f.setVisible(true); //可视
} 

//----------------------------------------------main方法
public static void main(String[] args){ 

MyCalculator p= new MyCalculator() ; //calculator 
p.launchFrame(); //窗体布局

} 
//============================================================关闭窗口响应类
class myWindowListener extends WindowAdapter
{       //关闭
		public void windowClosing(WindowEvent e){ 
		System.exit(0); 
		}
}

//=============================================================事件响应
public class guo_ActionListener implements ActionListener
{
	//--------------------------------------------
	public  void actionPerformed(ActionEvent e)	
	{
        Button gg=(Button)e.getSource();
			if (gg.getLabel()=="=")
			     {
			          calculate();
			          s=String.valueOf(a);
			         tf.setText(s);
			        Str="";
			     }
		   else if(gg.getLabel()=="+")
				   {
				         calculate();
				           tf.setText("");
				         Str="+";
				   }
		   else if(gg.getLabel()=="-")
				   {
				          calculate();
				         tf.setText("");
				         Str="-";
				   }
		   else if(gg.getLabel()=="*")
				   {
				    calculate();
				    tf.setText("");
				    Str="*";
				   }
			else if(gg.getLabel()=="/")
				   {
				    calculate();
				    tf.setText("");
				    Str="/";
				   }
			else if(gg.getLabel()=="%")
				   {
				    calculate();
				    tf.setText("");
				    Str="%";
				   }
			else if(gg.getLabel()=="说明")
				         {
				         	tf.setText("使用时运算符不显示,可累计运算");
				         }
				   
			else  if(gg.getLabel()=="Clean")
				   {
				   	 tf.setText("");
				    
				   }
		    else  if(gg.getLabel()=="作者")
				   tf.setText("05软测3班:郭顺旭,谢谢你的使用!");	   
			else
				   tf.setText(tf.getText()+gg.getLabel());
				   
				  
   }
 //-------------------------------------------------计算方法
   public void calculate()
     {
		   if(Str=="+")
		   {
		         a+=Double.valueOf(tf.getText());
		   }
		   else if(Str=="-")
		   {
		         a-=Double.valueOf(tf.getText());
		   }
		   else if(Str=="*")
		   {
		          a*=Double.valueOf(tf.getText());
		   }
		   else if(Str=="/")
		   {
		   	    while (Double.valueOf(tf.getText())== 0)
		   	    {
	               tf.setText("对不起,除数不能为零");
	            }
	          a/=Double.valueOf(tf.getText());
	       }  
   
   
		   else if(Str=="%")
		   {
		   	
			   	while (Double.valueOf(tf.getText())== 0) 
			   	{
		               tf.setText("对不起,除数不能为零");
		        }
		          a%=Double.valueOf(tf.getText());
		   }
		   else
		   {
		      a=Double.valueOf(tf.getText());
		   }
  }//calculate end
 }//class guo_ActionListener end
}//class MyCalculator end

⌨️ 快捷键说明

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