`

重现山寨计算器

阅读更多
来学习java语言也有十来天了,感觉有的时候挺没信心的,面对那些出过N次错误还继续的重犯,面对一个程序的一个小错误看了很久都没能找出来.不过特别高兴的是,在自己的努力过程中,还是收获到了自己比较满意的结果.在现在完成的两个小项目计算器和画板中,虽然功能上还是很局限,但是已经很满意了.毕竟我才刚刚开始接触编程之类的东东,刚入门嘛,适应总要有一个过程.慢慢来嘛!下面先来介绍我制作的山寨计算器.
   如果要完成计算器这个项目,其实并不难,不过我的建议就是先把这个实现的过程分为以下及部分:1,先创建一个计算器的界面;2,创建一个输入文字的文本框;3,创建所需要的按钮;4,给按钮设置命令,添加监听器;5,在监听器中实现算法;6,根据自己的爱好及需要,美化计算器.
    下面展示相关代码.

计算器类
    package jisuanqi;
/**
 * 创建一个计算器类
 */

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class jisuan {
	public static void main(String[]args){
		jisuan ji = new jisuan();
		ji.showUI();
	}
	/**
	 * 仔细化方法
	 */
	public void showUI(){
		JFrame jfr = new JFrame();
		jfr.setTitle("计算器Z.30");
		jfr.setSize(240,250);
	    java.awt.FlowLayout flo = new java.awt.FlowLayout();
		jfr.setLayout(flo);
		JTextField jt2 = new JTextField(20);
		jfr.add(jt2);
		jt2.setHorizontalAlignment(4);
		jisuanListener jis = new jisuanListener(jt2);
		for(int a=0;a<10;a++){
			JButton jbu = new JButton(a+"");
			jbu.setActionCommand(a+"");
			jbu.addActionListener(jis);
			jbu.setBackground(Color.CYAN);
			jfr.add(jbu);
			}

			
		//创建按钮 
		JButton jb = new JButton("+");
		JButton jb1 = new JButton("-");
		JButton jb2 = new JButton("*");
		JButton jb3 = new JButton("/");
		JButton jb0 = new JButton(".");
		JButton jb4 = new JButton("=");
		JButton jb5 = new JButton("C");
		JButton jb6 = new JButton("Backspace");
		JButton jb7 = new JButton("1/x");
		JButton jb8 = new JButton("+/-");
		JButton jb9 = new JButton("M+");
		JButton jb10 = new JButton("MR");
		JLabel jl = new JLabel("Author:Zhuzhenke");
		JLabel jl1 = new JLabel("Date:2013.01.30");
		/**
		 * 设置按钮颜色
		 */
		jb.setBackground(Color.LIGHT_GRAY);
		jb1.setBackground(Color.LIGHT_GRAY);
		jb2.setBackground(Color.LIGHT_GRAY);
		jb3.setBackground(Color.LIGHT_GRAY);
		jb0.setBackground(Color.LIGHT_GRAY);
		jb4.setBackground(Color.red);
		jb5.setBackground(Color.green);
		jb6.setBackground(Color.green);
		jb7.setBackground(Color.yellow);
		jb8.setBackground(Color.yellow);
		jb9.setBackground(Color.yellow);
		jb10.setBackground(Color.yellow);
		
		//设置按钮命令
		jb.setActionCommand("+");
		jb1.setActionCommand("-");
		jb2.setActionCommand("*");
		jb3.setActionCommand("/");
		jb4.setActionCommand("=");
		jb5.setActionCommand("C");
		jb6.setActionCommand("Backspace");
		jb7.setActionCommand("1/x");
		jb8.setActionCommand("+/-");
		jb0.setActionCommand(".");
		
		
		//给按钮添加监听器
		jb.addActionListener(jis);
		jb1.addActionListener(jis);
		jb2.addActionListener(jis);
		jb3.addActionListener(jis);
		jb4.addActionListener(jis);
		jb5.addActionListener(jis);
		jb6.addActionListener(jis);
		jb7.addActionListener(jis);
		jb8.addActionListener(jis);
		jb0.addActionListener(jis);
		
		//将按钮添加到窗体
		jfr.add(jb);
		jfr.add(jb1);
		jfr.add(jb2);
		jfr.add(jb3);
		jfr.add(jb0);
		jfr.add(jb4);
		jfr.add(jb5);
		jfr.add(jb6);
		jfr.add(jb7);
		jfr.add(jb8);
		jfr.add(jb9);
		jfr.add(jb10);
		jfr.add(jl);
		jfr.add(jl1);
		
		jfr.setVisible(true);
	

		}
}

监听器类
package jisuanqi;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
/**
 * 创建一个计算机监听器类
 * @author Administrator
 *
 */


public class jisuanListener implements ActionListener{
	public JTextField jt2;
	//构造jisuanji类的方法
	public jisuanListener (JTextField jt2){
		this.jt2 = jt2;
	}
	//重写方法
	public  void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		String ta = jt2.getText();
		if("C".contains(command)){
			jt2.setText("");
		}else if("=".equals(command)){
			//加法的运算
		    if(ta.contains("+")){                        				
				int index = ta.indexOf("+");
				String front = ta.substring(0,index);
				String end = ta.substring(index+1);
				float f = Float.parseFloat(front);
				float en = Float.parseFloat(end);
				float q = f+en;
				jt2.setText(q+"");
				//减法的运算
			}else if(ta.contains("-")){
				int index1 = ta.indexOf("-");
				String front1 = ta.substring(0,index1);
				String end1 = ta.substring(index1+1);
				float f1 = Float.parseFloat(front1);
				float e1 = Float.parseFloat(end1);
				float q1 = f1-e1;
				jt2.setText(q1+"");
				//乘法的运算
			}else if(ta.contains("*")) {
				int index2 = ta.indexOf("*");
				String front2 = ta.substring(0,index2);
				String end2 = ta.substring(index2+1);
				float f2 = Float.parseFloat(front2);
				float e2 = Float.parseFloat(end2);
				float q2 = f2*e2;
				jt2.setText(q2+"");
				//除法的运算
			}else if (ta.contains("/")){
				int index3 = ta.indexOf("/");
				String front3 = ta.substring(0,index3);
				String end3 = ta.substring(index3+1);
				float f3 = Float.parseFloat(front3);
				float e3 = Float.parseFloat(end3);
				float q3 = f3/e3;
				jt2.setText(q3+"");
			}else {//如果不含有运算符,就显示数字
				ta = ta+command;
				jt2.setText(ta);
					
			}
		}else if("Backspace".equals(command)){
				int index4 = ta.length();
				String front4 = ta.substring(0,index4-1);
				jt2.setText(front4+"");
		}else if("1/x".equals(command)){
			   int ta1 = Integer.parseInt(ta);
			   double q4 = 1/(double)ta1;
			   jt2.setText(q4+"");
	    }else if("+/-".equals(command)){
	    	int ta2 = Integer.parseInt(ta);
	    	int q5 =ta2/-1;
	    	jt2.setText(q5+"");
	    }else{
			ta = ta+command;
			jt2.setText(ta);
		}
		
	}
}


具体的解析过程在代码中有体现,不过在实现计算器项目时,在敲代码方面还是走了点弯路,例如在创建数字按钮时可以用for循环来实现.这项目当然还有不足的地方.比如说,还有在连续的计算中,如果是文本框中输入2+2+2时,计算器会不能运行,还有就是一些Windows中拥有的按钮功能不能实现.总之还得继续完善,继续加油!
  • 大小: 16.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics