`
小篮子java的家
  • 浏览: 32615 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

计算器的实现

阅读更多

------------简单计算器的实现--------------
package net.java.t071201;
 //使用import把JMenuBar引入到当前类,下边时候的时候就不需要在写包名,直接用类名就可以。

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

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 * 定义一个Counter类
 * 
 * @author 汤剑男
 */
 public class Counter {
	// 主函数
	public static void main(String[] args) {
		// 实例化一个对象
		Counter jf = new Counter();
		jf.display();
	}

	// 定义一个显示方法
	public void display(){
		javax.swing.JFrame a = new javax.swing.JFrame("计算器");
		a.setSize(400, 520);
		a.setResizable(false);
		a.setLocationRelativeTo(null);
		a.setDefaultCloseOperation(2);
		// java.awt.FlowLayout b =new java.awt.FlowLayout();
		a.setLayout(null);
		// 实例化一个菜单栏对象
		JMenuBar mb = new JMenuBar();
		// 实例化一个菜单项
		JMenu menu1 = new JMenu("查看(V)");

		// 实例化菜单子项
		JMenuItem menu1Item1 = new JMenuItem("打开");
		menu1.add(menu1Item1);

		JMenu menu2 = new JMenu("编辑(E)");
		JMenu menu3 = new JMenu("帮助(V)");
		// 把菜单添加到菜单栏中
		mb.add(menu1);
		mb.add(menu2);
		mb.add(menu3);
		// 设置窗体有菜单栏
		a.setJMenuBar(mb);
		// javax.swing.JLabel c = new javax.swing.JLabel("查看(V)");
		// c.setBounds(20, 0, 60, 30);
		// a.add(c);
		// javax.swing.JLabel d = new javax.swing.JLabel("编辑(E)");
		// d.setBounds(150, 0, 60, 30);
		// a.add(d);
		// javax.swing.JLabel e = new javax.swing.JLabel("帮助(H)");
		// e.setBounds(320, 0, 60, 30);
		// a.add(e);
		final javax.swing.JTextField f = new javax.swing.JTextField(20);
		f.setBounds(20, 20, 360, 80);
		a.add(f);
		javax.swing.JButton g = new javax.swing.JButton("MC");
		// 设置前景色
		g.setForeground(Color.RED);
		g.setBounds(10, 120, 60, 40);
		a.add(g);
		javax.swing.JButton h = new javax.swing.JButton("MR");
		h.setBounds(90, 120, 60, 40);
		a.add(h);
		javax.swing.JButton i = new javax.swing.JButton("MS");
		i.setBounds(170, 120, 60, 40);
		a.add(i);
		javax.swing.JButton j = new javax.swing.JButton("M+");
		j.setBounds(250, 120, 60, 40);
		a.add(j);
		javax.swing.JButton k = new javax.swing.JButton("M-");
		k.setBounds(330, 120, 60, 40);
		a.add(k);
		javax.swing.JButton g1 = new javax.swing.JButton("<—");
		g1.setBounds(10, 180, 60, 40);
		a.add(g1);
		javax.swing.JButton h1 = new javax.swing.JButton("CE");
		h1.setBounds(90, 180, 60, 40);
		a.add(h1);
		javax.swing.JButton i1 = new javax.swing.JButton("C");
		i1.setBounds(170, 180, 60, 40);
		i1.setActionCommand("C");
		a.add(i1);
		javax.swing.JButton j1 = new javax.swing.JButton("±");
		j1.setBounds(250, 180, 60, 40);
		a.add(j1);
		javax.swing.JButton k1 = new javax.swing.JButton("√");
		k1.setBounds(330, 180, 60, 40);
		a.add(k1);
		javax.swing.JButton g2 = new javax.swing.JButton("7");
		g2.setBounds(10, 240, 60, 40);
		g2.setActionCommand("7");
		a.add(g2);
		javax.swing.JButton h2 = new javax.swing.JButton("8");
		h2.setBounds(90, 240, 60, 40);
		h2.setActionCommand("8");
		a.add(h2);
		javax.swing.JButton i2 = new javax.swing.JButton("9");
		i2.setBounds(170, 240, 60, 40);
		i2.setActionCommand("9");
		a.add(i2);
		javax.swing.JButton j2 = new javax.swing.JButton("/");
		j2.setBounds(250, 240, 60, 40);
		j2.setActionCommand("/");
		a.add(j2);
		javax.swing.JButton k2 = new javax.swing.JButton("%");
		k2.setBounds(330, 240, 60, 40);
		a.add(k2);
		javax.swing.JButton g3 = new javax.swing.JButton("4");
		g3.setBounds(10, 300, 60, 40);
		g3.setActionCommand("4");
		a.add(g3);
		javax.swing.JButton h3 = new javax.swing.JButton("5");
		h3.setBounds(90, 300, 60, 40);
		h3.setActionCommand("5");
		a.add(h3);
		javax.swing.JButton i3 = new javax.swing.JButton("6");
		i3.setBounds(170, 300, 60, 40);
		i3.setActionCommand("6");
		a.add(i3);
		javax.swing.JButton j3 = new javax.swing.JButton("*");
		j3.setBounds(250, 300, 60, 40);
		j3.setActionCommand("*");
		a.add(j3);
		javax.swing.JButton k3 = new javax.swing.JButton("1/x");
		k3.setBounds(330, 300, 60, 40);
		a.add(k3);
		javax.swing.JButton g4 = new javax.swing.JButton("1");
		g4.setBounds(10, 360, 60, 40);
		g4.setActionCommand("1");
		a.add(g4);
		javax.swing.JButton h4 = new javax.swing.JButton("2");
		h4.setBounds(90, 360, 60, 40);
		h4.setActionCommand("2");
		a.add(h4);
		javax.swing.JButton i4 = new javax.swing.JButton("3");
		i4.setBounds(170, 360, 60, 40);
		i4.setActionCommand("3");
		a.add(i4);
		javax.swing.JButton j4 = new javax.swing.JButton("-");
		j4.setBounds(250, 360, 60, 40);
		j4.setActionCommand("-");
		a.add(j4);
		javax.swing.JButton k4 = new javax.swing.JButton("=");
		k4.setBounds(330, 360, 60, 100);
		k4.setActionCommand("=");
		a.add(k4);
		javax.swing.JButton g5 = new javax.swing.JButton("0");
		g5.setBounds(10, 420, 140, 40);
		g5.setActionCommand("0");
		a.add(g5);
		javax.swing.JButton h5 = new javax.swing.JButton(".");
		h5.setBounds(170, 420, 60, 40);
		h5.setActionCommand(".");
		a.add(h5);
		javax.swing.JButton i5 = new javax.swing.JButton("+");
		i5.setBounds(250, 420, 60, 40);
		i5.setActionCommand("+");
		a.add(i5);

		ActionListener al = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("=")) {
					items=1;
					operation(f);
					test=5;
					
				} else if (e.getActionCommand().equals("+")) {
					items=1;
					operation(f);
					test=1;
					
					
				} else if (e.getActionCommand().equals("-")) {
					items=1;
					operation(f);
					test=2;
					
				} else if (e.getActionCommand().equals("*")) {
					items=1;
					operation(f);
					test=3;
					

				} else if (e.getActionCommand().equals("/")) {
					items=1;
					operation(f);
					test=4;
					} 
				else if(e.getActionCommand().equals("C")){
					f.setText("");
					 count = 0;
				     items =0;
					 test=0;
					 temp=0;
				}
				else {
					if(items==0){
					String str = f.getText();
					str += e.getActionCommand();
					f.setText(str);// 为什么要把f定义成final
					}
					else{
						f.setText("");
						String str = e.getActionCommand();
						f.setText(str);
						items=0;
					}
					
				
				}
			}

		};
		i1.addActionListener(al);
		g2.addActionListener(al);
		h2.addActionListener(al);
		i2.addActionListener(al);
		j2.addActionListener(al);
		g3.addActionListener(al);
		h3.addActionListener(al);
		i3.addActionListener(al);
		j3.addActionListener(al);
		g4.addActionListener(al);
		h4.addActionListener(al);
		i4.addActionListener(al);
		j4.addActionListener(al);
		k4.addActionListener(al);
		g5.addActionListener(al);
		h5.addActionListener(al);
		i5.addActionListener(al);
		a.setVisible(true);
	}
	public final void operation(javax.swing.JTextField f){
		if (count == 0) {
			// 得到点击加号之前文本框中的值
			value1 = f.getText();
			temp = Integer.parseInt(value1);
			count++;
		} else if (count >= 1) {
			value2 = f.getText();
			if(test==1){
			  temp =temp+ Integer.parseInt(value2);
			  f.setText( temp + "");
			  count++;}
			else if(test==2){ temp =temp-Integer.parseInt(value2);
			  f.setText( temp + "");
			  count++;	
			}
			else if(test==3){temp =temp*Integer.parseInt(value2);
			  f.setText( temp + "");
			  count++;
         }
			else if (test==4){
				temp =temp/Integer.parseInt(value2);
			  f.setText( temp + "");
			  count++;	
			}
			else if (test==5){
			  f.setText( temp + "");
			  count++;	
			}
			}
	}
	// 存储值的
	private String value1;
	private String value2;
	// 计数器
	private int count = 0;
	private int items =0;
	private int test=0;
	private int temp=0;
}

  • 大小: 39.1 KB
  • 大小: 42.7 KB
  • 大小: 39.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics