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

📄 calculator.java

📁 说明在java压缩包中
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame{
    public Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
    public Frame fr;
    public Label l;
    public boolean flag;     //用于判断是否为数字的开始
    public String lastCommand;//保存+,1/x,*,/,=命令
	public double result;   //保存计算结果
  //初始化函数
    public void init(){
    	flag=true;           //初始化信息
    	lastCommand="=";
       	result=0.0;
    	fr=new Frame("a Simple Calculator!");
    	fr.setSize(210,210);
    	fr.setBackground(Color.gray);
    	l=new Label("0.0");
       	Panel p1=new Panel(new GridLayout(4,4,2,2)); //对按钮进行排版
       	Panel p2=new Panel(new GridLayout(1,1));
       	p1.setBackground(Color.gray);
		b1=new Button("0");  //数字按钮
		b2=new Button("1");
		b3=new Button("2");
		b4=new Button("3");
		b5=new Button("4");
		b6=new Button("5");
		b7=new Button("6");
		b8=new Button("7");
		b9=new Button("8");
		b10=new Button("9");
		b11=new Button("+");  //控制命令
		b12=new Button("1/x");
		b13=new Button("*");
		b14=new Button("/");
		b15=new Button(".");
		b16=new Button("=");
		b17=new Button("C");
  //    设置字体颜色
		b1.setForeground(Color.blue);
		b2.setForeground(Color.blue);
		b3.setForeground(Color.blue);
		b4.setForeground(Color.blue);
		b5.setForeground(Color.blue);
		b6.setForeground(Color.blue);
		b7.setForeground(Color.blue);
		b8.setForeground(Color.blue);
		b9.setForeground(Color.blue);
		b10.setForeground(Color.blue);
		b11.setForeground(Color.blue);
		b12.setForeground(Color.blue);
		b13.setForeground(Color.blue);
		b14.setForeground(Color.blue);
		b15.setForeground(Color.blue);
		b16.setForeground(Color.blue);
		b17.setForeground(Color.blue);
		b1.addActionListener(new ActionListener1());//添加监听
		b2.addActionListener(new ActionListener1());
		b3.addActionListener(new ActionListener1());
		b4.addActionListener(new ActionListener1());
		b5.addActionListener(new ActionListener1());
		b6.addActionListener(new ActionListener1());
		b7.addActionListener(new ActionListener1());
		b8.addActionListener(new ActionListener1());
		b9.addActionListener(new ActionListener1());
		b10.addActionListener(new ActionListener1());
		b11.addActionListener(new ActionListener2());
		b12.addActionListener(new ActionListener2());
		
		b15.addActionListener(new ActionListener1());
		b16.addActionListener(new ActionListener2());
		b17.addActionListener(new ActionListener3());
    	p1.add(b1);           //添加到Panel
    	p1.add(b2);
    	p1.add(b3);
    	p1.add(b4);
    	p1.add(b5);
    	p1.add(b6);
    	p1.add(b7);
    	p1.add(b8);
    	p1.add(b9);
    	p1.add(b10);
    	p1.add(b11);
    	p1.add(b12);
    	p1.add(b13);
    	p1.add(b14);
    	p1.add(b15);
    	p1.add(b16);
    	p2.add(b17);
    	fr.add(p1,BorderLayout.CENTER);
    	fr.add(p2,BorderLayout.SOUTH);
    	fr.add(l,BorderLayout.NORTH);
    	fr.addWindowListener(new WindowAdapter()       //关闭窗口
    	{
    		 public void windowClosing(WindowEvent e)
            {
              System.exit(0);
            }
        });
    	fr.setVisible(true);
      	//System.out.println(fr.getSize() + "     ");
        }
    
      //将数字按钮和控制按钮分开进行监听
    public class ActionListener1 implements ActionListener
    {
     public void actionPerformed(ActionEvent e)
      {
         String in=e.getActionCommand();   //得到当前按钮值
         if (flag)                         
         {
           l.setText("");                 //用于第二次或以后输入数据时的清空
           flag=false;
           l.setText(in);
          }
         else
           l.setText(l.getText()+in);
         }
    }

     public class ActionListener2 implements ActionListener
     {
       public void actionPerformed(ActionEvent e)
       {
          String command=e.getActionCommand();
         if(flag)
          {
           display(Double.parseDouble(l.getText()));
           lastCommand=command;          //被保存下来的控制符号(+,1/x,*,/,=)
          }
          else
          {
          display(Double.parseDouble(l.getText())); //转化类型
          lastCommand=command;
          flag=true;
          }
         }
      }
      
   //清空键监听   
   public class ActionListener3 implements ActionListener
     {
       public void actionPerformed(ActionEvent e)
       {
           l.setText("0.0");
           lastCommand="=";
           flag=true;
           result=0.0;
        }
      }
    //进行计算,并在Label中显示
	public void display(double x){
	  if (lastCommand.equals("+")) 
	        result+= x;    
      else if (lastCommand.equals("1/x")) 
            result=1/x;
      else if (lastCommand.equals("=")) 
            result=x;
      l.setText(""+ result);
	}
	
	public static void main(String[] args){
		Calculator c=new Calculator();
		c.init();
	}
}

⌨️ 快捷键说明

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