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

📄 mycalculator.java

📁 java编写的一个小程序(计算器程序 很多人编程过程好像是必不可少的一种游戏)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				return 0;
			}
                                                else  temp=Math.sqrt(st);
			return temp;
		                
		default:
			error=true;
			System.out.println("False_F()!");                                                
                                                return 0;
	}
 }

 //以上是基本的算法,下面是图形界面的实现

 public static void main(String [] args){
  final Frame f=new Frame("My Calculator");
  Monitor2 f2=new Monitor2(f);                              //关闭窗口事件监听
  f.addWindowListener(f2);
  
  output.addTextListener(new TextListener(){                //文本更改事件监听
    public void textValueChanged(TextEvent e){
        String afterChang=((TextField)e.getSource()).getText();
        afterChang=afterChang+'\0';
        input=afterChang.toCharArray();
    }
  });
  output.setBackground(Color.pink);
  output.addActionListener(new ActionListener(){              //文本动作事件监听
    public void actionPerformed(ActionEvent e){
        lookahead=yylex();
        output.setText(""+E());
    }
  });
  
  Panel p1=new Panel();                       //定义按钮
  p1.setLayout(new GridLayout(1,1));
  p1.setSize(400,100);
  p1.add(output);
  f.setBackground(Color.pink);  
         //f.setBackgroud(new Color(120,175,175));
  f.add(p1,BorderLayout.NORTH);
  f.add(new Label(),BorderLayout.WEST);
  f.add(new Label(), BorderLayout.EAST);
  MenuBar mb=new MenuBar();              //定义菜单
  f.setMenuBar(mb);
  Menu m1=new Menu("Edit");
  Menu m2=new Menu("Help");
  mb.add(m1);
  mb.setHelpMenu(m2);
  MenuItem m12=new MenuItem("Restart",new MenuShortcut('s'));
  MenuItem m11=new MenuItem("Exit",new MenuShortcut('x'));
  MenuItem m13=new MenuItem("BackSpace",new MenuShortcut('c'));
  MenuItem m21=new MenuItem("Police",new MenuShortcut('p'));
  MenuItem m14=new MenuItem("弧度",new MenuShortcut('h'));
  MenuItem m15=new MenuItem("角度",new MenuShortcut('j'));
  m1.add(m12);
  m1.addSeparator();
  m1.add(m13);
  m1.addSeparator();
  m1.add(m14);
  m1.addSeparator();
  m1.add(m15);
  m1.addSeparator();
  m1.add(m11);
  m2.add(m21);
  m11.addActionListener( new ActionListener(){          //退出菜单
    public void actionPerformed(ActionEvent e){
      System.exit(0);
    }
  });
  m12.addActionListener( new ActionListener(){            //重新开始菜单
    public void actionPerformed(ActionEvent e){
      pCur=0;
      ip=0;
      s0="";
      output.setText("Please restart!");
    }
  });
  m13.addActionListener( new ActionListener(){           //BackSpace菜单
    public void actionPerformed(ActionEvent e){
      s0=s0.substring(0,ip);
      output.setText(s0);
      ip--;
    }
  });
  m14.addActionListener( new ActionListener(){            //弧度制
    public void actionPerformed(ActionEvent e){
      pi=180;
    }
  });
  m15.addActionListener( new ActionListener(){            //角度制
    public void actionPerformed(ActionEvent e){
      pi=Math.PI;
    }
  });
  
  m21.addActionListener( new ActionListener(){            //帮助中的说明
    public void actionPerformed(ActionEvent e){
      
      Dialog d=new Dialog(f,"Police",false);
      Label pro1=new Label("此乃小型计算器,只能实现基本的算法,");
      Label pro2=new Label("如加减乘除,正三角函数(sin,cos,tan),");
      Label pro3=new Label("开方(sqrt),平方(pow)和自然对数(ln,lg)!");
      d.add(pro1,BorderLayout.NORTH);
      d.add(pro2,BorderLayout.CENTER);
      d.add(pro3,BorderLayout.SOUTH);
      d.setSize(250,200);   
      d.setBackground(Color.pink);
      d.setVisible(true);
      Monitor3 d3=new Monitor3(d);
      d.addWindowListener(d3);      
    }
  });
  Panel p=new Panel();
  p.setLayout(new GridLayout(4,7));
  Font MyFont = new Font("TimesRoman", Font.BOLD, 20);               //按扭的定义
  Button b25 = new Button("ln");
  Button b26 = new Button("E");
  Button b27 = new Button("PI");
  Button b28 = new Button("lg");
  Button b1=new Button("+");
  Button b2=new Button("1");
  Button b3=new Button("2");
  Button b4=new Button("3");
  Button b5=new Button("pow");
  Button b6=new Button("sin");
  Button b7=new Button("-");
  Button b8=new Button("4");
  Button b9=new Button("5");
  Button b10=new Button("6");
  Button b11=new Button("sqrt");
  Button b12=new Button("cos");
  Button b13=new Button("*");
  Button b14=new Button("7");
  Button b15=new Button("8");
  Button b16=new Button("9");
  Button b17=new Button("+/-");
  Button b18=new Button("tan");
  Button b19=new Button("/");
  Button b20=new Button("0");
  Button b21=new Button(".");
  Button b22=new Button("=");
  Button b23=new Button("(");
  Button b24=new Button(")");

  b1.setBackground(Color.blue);                              //颜色
  b2.setBackground(Color.cyan);
  b3.setBackground(Color.cyan);
  b4.setBackground(Color.cyan);
  b5.setBackground(Color.magenta);
  b6.setBackground(Color.magenta);
  b7.setBackground(Color.blue);
  b8.setBackground(Color.cyan);
  b9.setBackground(Color.cyan);
  b10.setBackground(Color.cyan);
  b11.setBackground(Color.magenta);
  b12.setBackground(Color.magenta);
  b14.setBackground(Color.cyan);
  b18.setBackground(Color.magenta);
  b15.setBackground(Color.cyan);
  b13.setBackground(Color.blue);
  b16.setBackground(Color.cyan);
  b17.setBackground(Color.blue);
  b19.setBackground(Color.blue);
  b22.setBackground(Color.red);
  b21.setBackground(Color.cyan);
  b24.setBackground(Color.blue);
  b20.setBackground(Color.cyan);
  b23.setBackground(Color.blue);
 b25.setBackground(Color.magenta);
 b28.setBackground(Color.magenta);
 b26.setBackground(Color.cyan);
 b27.setBackground(Color.cyan);
  

  b1.setFont(MyFont);             //setFont,字体
  b2.setFont(MyFont);
  b3.setFont(MyFont);
  b4.setFont(MyFont);
  b5.setFont(MyFont);
  b6.setFont(MyFont);
  b7.setFont(MyFont);
  b8.setFont(MyFont);
  b9.setFont(MyFont);
  b10.setFont(MyFont);
  b11.setFont(MyFont);
  b12.setFont(MyFont);
  b14.setFont(MyFont);
  b18.setFont(MyFont);
  b15.setFont(MyFont);
  b13.setFont(MyFont);
  b16.setFont(MyFont);
  b17.setFont(MyFont);
  b19.setFont(MyFont);
  b22.setFont(MyFont);
  b21.setFont(MyFont);
  b24.setFont(MyFont);
  b20.setFont(MyFont);
  b23.setFont(MyFont);
  b25.setFont(MyFont);
  b26.setFont(MyFont);
  b27.setFont(MyFont);
  b28.setFont(MyFont);
  
  

  Monitor1 bh=new Monitor1();          //动作事件监听
  b1.addActionListener(bh);
  b2.addActionListener(bh);
  b3.addActionListener(bh);
  b4.addActionListener(bh);
  b5.addActionListener(bh);
  b6.addActionListener(bh);
  b7.addActionListener(bh);
  b8.addActionListener(bh);
  b9.addActionListener(bh);
  b10.addActionListener(bh);
  b11.addActionListener(bh);
  b12.addActionListener(bh);
  b13.addActionListener(bh);
  b14.addActionListener(bh);
  b15.addActionListener(bh);
  b16.addActionListener(bh);
  b17.addActionListener(bh);
  b18.addActionListener(bh);
  b19.addActionListener(bh);
  b20.addActionListener(bh);
  b21.addActionListener(bh);
  b22.addActionListener(bh);
  b23.addActionListener(bh);
  b24.addActionListener(bh);
  b25.addActionListener(bh);
  b26.addActionListener(bh);
  b27.addActionListener(bh);
  b28.addActionListener(bh);
  
  p.add(b1);                              //加入Panel中
  p.add(b2); 
  p.add(b3);
  p.add(b4);
  p.add(b26);
  p.add(b5);
  p.add(b6);
  p.add(b7);
  p.add(b8); 
  p.add(b9);
  p.add(b10);
  p.add(b27);
  p.add(b11);
  p.add(b12); 
  p.add(b13);
  p.add(b14);
  p.add(b15);
  p.add(b16);
  p.add(b25);
  p.add(b28);
  p.add(b18);
  p.add(b19);
  p.add(b20); 
  p.add(b21);
  p.add(b22);
  p.add(b17); 
  p.add(b23);
  p.add(b24);
  
  
  MyThread myThread = new MyThread();
  Thread t1  = new Thread(myThread);
  t1.start();
  
  
  //l4.setBackground(Color.cyan);
  f.add(l4, BorderLayout.SOUTH);
  f.add(p,BorderLayout.CENTER);
  f.setSize(340,260);
  f.setVisible(true);   
 }
 static class MyThread implements Runnable{
	private String l0="Local's time is: ";
                //boolean flg=true;
	public MyThread(){ }
	public void run(){
                             while(true){
                                  l0=l0+(new Date());
                                  l4.setText(l0);                                    
                                  try { Thread.sleep(1000);
                                          l0="Local's time is:";
		  }catch(InterruptedException e){}}
	    }
                		
	
 }

 static class Monitor2 extends WindowAdapter{             //关闭窗口
   Monitor2(Frame f2){
     f=f2;
   }
   private Frame f;
   public void windowClosing(WindowEvent e){   
       System.exit(0);   
   }   
 }
 static class Monitor3 extends WindowAdapter{               //关闭对话框
   Monitor3(Dialog d3){
     d=d3;
   }
   private Dialog d;
   public void windowClosing(WindowEvent e){   
       d.dispose();   
   }   
 }
 public static class Monitor1 implements ActionListener           //按钮动作监听
 { 
   public void actionPerformed(ActionEvent e){
     String s=e.getActionCommand();
     ip=s0.length();
	 if (s.equals("+/-"))
	 {
		 if (i == 0)
		 {
			 s0 = s0 + "-";
			 i++;
		 }
		 else i--;
	 }
	 else if (s.equals("E")){
		 double x = Math.E;
		 s0 = s0 + x;
	 }
	 else if (s.equals("PI"))
		 s0 = s0 + Math.PI;
	 
	 else if (s.equals("="))                               //此时运行上面的算法
	 {
		 input = (s0 + "\0").toCharArray();
		 lookahead = yylex();
		 double x = E();
		 if (error == true)
			 s0 = "Error!";
		 else s0 = x + "";
		 input = (s0 + "\0").toCharArray();         
		 pCur = 0;                     //得出的结果可以继续计算                   
		 error = false;

	 }
	 else s0 = s0 + s;

     output.setText(s0);                           //在文本中显示算式和结果
   } 
 }
 
}









⌨️ 快捷键说明

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