• Java语言编写计算器(简单的计算器)


    Java编写的一个简单计算器,本人还比较菜,只能这样了,有点代码冗余,不能连续计算。

    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    public class calculate extends JFrame {
    	JButton[] b = new JButton[10];
    	JPanel panel1 = new JPanel(); // 实例化面板
    	JPanel panel2 = new JPanel(); 
    	JPanel panel3 = new JPanel();
    	JTextField answer=new JTextField(10);
    	JButton clear=new JButton("清除");
    	JButton equal=new JButton("=");
    	JButton point=new JButton(".");
    	JButton plus=new JButton("+");
    	JButton minus=new JButton("-");
    	JButton multi=new JButton("×");
    	JButton division=new JButton("÷");
    	String foreText=new String("");
    	String backText=new String("");
    	String s=new String();
    	String s1=new String();
    	Object op=new String();
    	Object o=new String();
    	double d, dFore, dBack;
    	int i=9;
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		JFrame calculator =new calculate();
    		calculator.setVisible(true);
        
    	}
    public calculate(){
    	setTitle("计算器");
    	setSize(300,300);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
    	Toolkit kit = Toolkit.getDefaultToolkit();
    	Dimension screenSize = kit.getScreenSize();//获取屏幕分辨率
    	setLocation(screenSize.width/4,screenSize.height/4);//位置
    	for ( i =9; i>=0; i--) {
    		b[i] = new JButton(Integer.toString(i));// 实例化数字按钮 
    		b[i].addActionListener(new MyListener1());
    		panel2.add(b[i]); // 增加按钮到面板
    		
    	}
    	
    	setLayout(new FlowLayout()); // 设置布局管理器 
    	panel1.setLayout(new FlowLayout());
    	panel2.setLayout(new GridLayout(4,3,10,8));
    	panel3.setLayout(new GridLayout(4,1,8,12)); 
    	clear.setFont(new Font("隶书",Font.PLAIN,15));
    	clear.setForeground(Color.RED);
    	clear.addActionListener(new MyListener1());
    	equal.setForeground(Color.RED);
    	equal.addActionListener(new MyListener1());
    	plus.setForeground(Color.RED);
    	plus.addActionListener(new MyListener1());
    	minus.setForeground(Color.RED);
    	minus.addActionListener(new MyListener1());
    	multi.setForeground(Color.RED);
    	multi.addActionListener(new MyListener1());
    	division.setForeground(Color.RED);
    	division.addActionListener(new MyListener1());
    	equal.setFont(new Font("隶书",Font.PLAIN,20));
    	plus.setFont(new Font("隶书",Font.PLAIN,18));
    	minus.setFont(new Font("隶书",Font.PLAIN,18));
    	multi.setFont(new Font("隶书",Font.PLAIN,18));
    	division.setFont(new Font("隶书",Font.PLAIN,18));
    	point.addActionListener(new MyListener1());
    	panel1.add(answer);
    	panel1.add(clear);
    	panel2.add(point);
    	panel2.add(equal);
    	panel3.add(plus);
    	panel3.add(minus);
    	panel3.add(multi);
    	panel3.add(division);
    	add(panel1); 
    	add(panel2);
    	add(panel3);
    }
    
     class MyListener1 implements ActionListener{
    	public void actionPerformed(ActionEvent e){
    		
    		for(int i=0;i<=9;i++) {
    			if(e.getSource()==b[i]||e.getSource()==point){
    				answer.setText("");
    				if(e.getSource()==point){
    					
    					s=foreText+".";
    					answer.setText(s);
    				}else{
    				if(s!=""){
    					answer.setText(s+b[i].getText());
    					foreText=s+b[i].getText();//此处应当修改,
    				}else{
    					answer.setText(foreText+b[i].getText());
    				foreText=foreText+b[i].getText();
    				}
    				}
    				}
    		}
    		if(e.getSource()==clear){
    			foreText="";
    			s="";
    			answer.setText("");
    		}
    		if(e.getSource()==plus||e.getSource()==minus||e.getSource()==multi||e.getSource()==division){
    			dFore=Double.parseDouble(foreText);
    			s="";
    			foreText="";
    			answer.setText("");
    			op=e.getSource();
    		}
    		if(e.getSource()==equal){
    			dBack=Double.parseDouble(foreText);
    			foreText="";
    			answer.setText("");
    			if(op==plus){
    				d=dFore+dBack;
    			}
    			if(op==minus){
    				d=dFore-dBack;
    			}
    			if(op==multi){
    				d=dFore*dBack;
    			}
    			if(op==division){
    				d=dFore/dBack;
    			}
    			answer.setText(""+d);
    		}
    	}
    }
    }
     
    
    
    

    运行截图





  • 相关阅读:
    iOS开发-图片高斯模糊效果
    很认真地聊一聊程序员的自我修养
    iOS 动画队列—仿映客刷礼物效果
    iOS -NSOperation并发编程
    iOS Status Bar变换
    iOS 使用Quartz和OpenGL绘图
    iOS开发中WiFi相关功能总结
    视频直播初窥:高仿喵播APP
    智捷公开课马上开始了-欢迎大家一起讨论学习-第一系列读《Swift开发指南(修订版) 》看Swift视频教程
    《iOS开发指南》要改iOS8版本了,听听您的意见?
  • 原文地址:https://www.cnblogs.com/xiaoduc-org/p/5327260.html
Copyright © 2020-2023  润新知