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

📄 calc.java

📁 用JAVA实现的简单计算器
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 class Calc extends JFrame{
	Label text;
	int i;
		Double x,y,sum;     //必须在外面定义。。。在里面会认为可能未被初始化
		String op;
	Button[] b=new Button[10];
	public Calc(){
		super("jin calc for jin's first java");
		text=new Label("",Label.RIGHT);
		
	    Panel p1=new Panel();
		Panel p2=new Panel();
		Panel p3=new Panel();
		p1.add(text);
		p1.setBounds(1,1,200,20);
	    p2.setBackground(new Color(0,0,0));
	    p2.setLayout(new GridLayout(3,2));
	    p3.setLayout(new GridLayout(4,1));
	    for(i=0;i<10;i++){
	    	b[i]=new Button(""+i+"");
	    
	    	p2.add(b[i]);
	    	b[i].addActionListener(new ActionHandler());
	    	}
	     Button s2=new Button("C");
	     Button s1=new Button("=");
	     s1.addActionListener(new ActionHandler());
	     s2.addActionListener(new ActionHandler());
	     p2.add(s2);
	     p2.add(s1);
	     Button j1=new Button("+");
	     Button j2=new Button("-");
	     Button j3=new Button("*");
	     Button j4=new Button("/");
	     j1.addActionListener(new ActionHandler());
	     j2.addActionListener(new ActionHandler());
	     j3.addActionListener(new ActionHandler());
	     j4.addActionListener(new ActionHandler());
	    p3.add(j1);
	     p3.add(j2);
	     p3.add(j3);
	     p3.add(j4);
	    add(text,BorderLayout.NORTH);
	    add(p2,BorderLayout.CENTER);
	    add(p3,BorderLayout.EAST);
		}
	public static void main (String[] args) {
    Calc my=new Calc();
    my.setSize(300,300);
    my.setVisible(true);
    System.out.println("13131351");
    }
	
class ActionHandler implements ActionListener{     //监听实现   缺少C
		public void actionPerformed(ActionEvent e){
        	if((e.getActionCommand()=="+")||(e.getActionCommand()=="-" )||(e.getActionCommand()=="*" )||(e.getActionCommand()=="/" ))
			{op=e.getActionCommand();
			x=Double.parseDouble(text.getText());
			text.setText("");
				}
				 else if(e.getActionCommand()=="=" ){
					y=Double.parseDouble(text.getText());
			if(op=="+") sum=x+y;
			if(op=="-") sum=x-y;
			if(op=="*") sum=x*y;
			if(op=="/") sum=x/y;
			
					text.setText(""+sum+"");
					}
					else text.setText(text.getText()+e.getActionCommand());
			}
			}
		
	}
	
	
	
	
	

⌨️ 快捷键说明

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