• 用Java设计简易的计算器


    运行初始状态:

    计算结果如下:

     

    代码如下:

    package jisuanqi;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    
    class counter1 extends JFrame 
    {
    public counter1()
    	 {
    	super("计算器");
        this.setSize(400,100);
        this.setLocation(300,240);
        this.setLayout(new FlowLayout());   //确定窗口格式为流输入
        TextField text1=new TextField(4);
        text1.setText("1");
        this.add(text1);
        
        String proList[] = { "+","-","x" ,"%"};   //将"+","-","x" ,"%"作为列表元素
        TextField text;
        JComboBox comboBox;         //创建下拉复选框
        Container conPane = getContentPane();   //创建一个名为conPane的容器
        comboBox = new JComboBox(proList);    //把列表元素加入下拉复选框
        comboBox.setEditable(true);     //使复选框变为可操作
        conPane.add(comboBox);    //将复选框加入容器中
        
        TextField text2=new TextField(4);
        text2.setText("1");
        this.add(text2);
        JButton button = new JButton("=");
        this.add(button);
        TextField text3=new TextField(4);
        text3.setText("2");
        
    
        button.addActionListener(new ActionListener(){    //添加按钮监听事件
        	public void actionPerformed(ActionEvent e)    //创建事件响应函数
        	{ 
        		String s=comboBox.getEditor().getItem().toString();    //获取复选框中的元素
        		double a= Integer.parseInt(text1.getText());     //将两个文本框中的字符串强制转换成浮点型,以便于之后的计算
        		double b= Integer.parseInt(text2.getText());
        		if(s.equals("+")) {    //判断复选框中的元素种类
        			double t=a+b;
        			String m=String.valueOf(t);  //由于文本框中的数据流只能为字符串,这里就需要将计算得到的浮点型数据强制转换成字符串型
        			
        			text3.setText(m);    //将最后的结果放在文本框中
        		}
        		else if(s.equals("-"))  //后面的与之前的类似,不在注释
        		{double t=a-b;
    			String m=String.valueOf(t);
    			
    			text3.setText(m);}
        		else if(s.equals("x"))
        		{double t=a*b;
        		String m=String.valueOf(t);
    			
    			text3.setText(m);}
        		else
        		{double t=a/b;
        		String m=String.valueOf(t);
    			
    			text3.setText(m);}
        	
        	}});
        conPane.add(text3);
        this.setVisible(true);    //将窗口设置为可视化
    	 }
    
    
    
    }
    
    public class Counter {
    	public static void main(String[] args)
    	{
    			new counter1();
    			}
    }
    

      

     

    修改版如下:

     

     

    源码如下:

    package jisuanqi;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    class counter1 extends JFrame 
    {
    public counter1()
    	 {
    	super("计算器");
        this.setSize(400,130);
        this.setLocation(300,240);
        this.setLayout(new FlowLayout());   //确定窗口格式为流输入
        TextField text1=new TextField(4);
        text1.setText("1");
        JLabel label1 = new JLabel("");
        text1.addFocusListener(new FocusListener() {  //添加焦点事件监听器
    		public void focusGained(FocusEvent arg0) {   //设置获得焦点时的事件
    			label1.setText("正在输入中...");
    			
    		}
    		public void focusLost(FocusEvent arg0) {     //设置失去焦点时的事件
    			if(!text1.getText().matches("\d+"))  //使用正则表达式判断该字符串是否为数字,第一个是转义符,d+表示匹配1个或 //多个连续数字,"+"和"*"类似,"*"表示0个或多个
    				label1.setText("输入的第一个数据有误");
    			else label1.setText("");
    			
    		}
        });
        this.add(text1);
      
        String proList[] = { "+","-","x" ,"%"};   //将"+","-","x" ,"%"作为列表元素
        TextField text;
        JComboBox comboBox;         //创建下拉复选框
        Container conPane = getContentPane();   //创建一个名为conPane的容器
        comboBox = new JComboBox(proList);    //把列表元素加入下拉复选框
        comboBox.setEditable(true);     //使复选框变为可操作
        conPane.add(comboBox);    //将复选框加入容器中
        
        TextField text2=new TextField(4);
        text2.setText("1");
        this.add(text2);
        text2.addFocusListener(new FocusListener() {
    		public void focusGained(FocusEvent arg0) {
    			label1.setText("正在输入中...");
    			
    		}
    		public void focusLost(FocusEvent arg0) {
    			if(!text2.getText().matches("\d+"))
    				label1.setText("输入的第二个数据有误");
    			else label1.setText("");
    			
    		}
        });
        JButton button = new JButton("=");
        this.add(button);
        TextField text3=new TextField(4);
        text3.setText("2");
        
    
        button.addActionListener(new ActionListener(){    //添加按钮监听事件
        	public void actionPerformed(ActionEvent e)    //创建事件响应函数
        	{ 
        		String s=comboBox.getEditor().getItem().toString();    //获取复选框中的元素
        		double a= Integer.parseInt(text1.getText());     //将两个文本框中的字符串强制转换成浮点型,以便于之后的计算
        		double b= Integer.parseInt(text2.getText());
        		if(s.equals("+")) {    //判断复选框中的元素种类
        			double t=a+b;
        			String m=String.valueOf(t);  //由于文本框中的数据流只能为字符串,这里就需要将计算得到的浮点型数据强制转换成字符串型
        			
        			text3.setText(m);    //将最后的结果放在文本框中
        		}
        		else if(s.equals("-"))  //后面的与之前的类似,不在注释
        		{double t=a-b;
    			String m=String.valueOf(t);
    			
    			text3.setText(m);}
        		else if(s.equals("x"))
        		{double t=a*b;
        		String m=String.valueOf(t);
    			
    			text3.setText(m);}
        		else
        		{double t=a/b;
        		String m=String.valueOf(t);
    			
    			text3.setText(m);}
        	
        	}});
        conPane.add(text3);
        this.add(label1);
        this.setVisible(true);    //将窗口设置为可视化
    	 }
    
    
    
    }
    
    public class Counter {
    	public static void main(String[] args)
    	{
    			new counter1();
    			}
    }
    

      

    总结心得:

    (1)在创建选项框时,要将所有的选项放在一个“容器”里,并把这个容器添加到程序中,这里我用的容器为JComboBox comboBox,同时需要导入包javax.swing.ComboBoxEditor和javax.swing.JComboBox;

    (2)由于设置了按钮响应功能,所以要设置按键动作和响应,这里导入了包java.awt.event.ActionEvent和java.awt.event.ActionListener

    (3)因为文本框中输入读取到的是字符串,所以要进行计算时,要先将其转为整形,在文本框输出时,同理要将整形转换为字符串

    (4)注意:当光标移动到文本框上面时,获得焦点事件,当光标移走时,便失去焦点事件,所以要注意两个函数的作用。

  • 相关阅读:
    团队任务3:每日例会(2018-10-24)
    团队任务3:每日例会(2018-10-23)
    团队任务3:每日例会(2018-10-22)
    团队任务3:每日例会(2018-10-19)
    团队任务3:每日例会(2018-10-18)
    团队任务3:每日立会(2018-10-16)
    团队任务3:每日立会(2018-10-17)
    课后作业4:个人总结
    课后作业3:软件分析与用户体验分析
    团队任务3 每日立会
  • 原文地址:https://www.cnblogs.com/fjcy/p/10939931.html
Copyright © 2020-2023  润新知