《用java编写地计算器基本功能可以实现.doc》由会员分享,可在线阅读,更多相关《用java编写地计算器基本功能可以实现.doc(11页珍藏版)》请在课桌文档上搜索。
1、wordJsp.javapackage 练习题;import java.awt.Button;import java.awt.Color;import java.awt.Font;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.accessibility.AccessibleContext;import javax.swing.JFrame;public class Jsp extends JFrame /* * 声明计算器
2、里的各种组件 * */private TextField tf_show;/ 显示框private Button b_0;/ 数字0private Button b_point;/ 点private Button b_add;/ 加法运算符private Button b_1;/ 数字1private Button b_2;/ 数字2private Button b_3;/ 数字3private Button b_sub;/ 减号private Button b_equal;/ 等号运算符private Button b_4;/ 数字4private Button b_5;/ 数字5priva
3、te Button b_6;/ 数字6private Button b_mul;/ 乘号private Button b_fra;/ 分数private Button b_7;/ 数字7private Button b_8;/ 数字8private Button b_9;/ 数字9private Button b_div;/ 除号private Button b_mod;/ 取余private Button b_back;/ 退格private Button b_ce;/ 去除private Button b_c;/private Button b_sign;/ 加减号private Butt
4、on b_sqrt;/ 根号private Button b_mc;/ MCprivate Button b_mr;/ MRprivate Button b_ms;/ MSprivate Button b_madd;/ M+private Button b_msub;/ M-private static final Color C_BUTTON = new Color(0, 0, 255);private String opt;/ 记录用户点击的操作private double num1;/ 用来记录被操作数当点击任何一个操作符时private double num2;/ 用来记录操作数/ 标
5、识量:标志显示框是追加模式还是替换模式/ 值为true:替换 false:追加private boolean b1 = true;/private int i = 1;private Font f_show = new Font(黑体, Font.BOLD, 18);/ 设置文本框加粗public Jsp(String title) super(title);this.setBounds(300, 300, 242, 320);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLayout(null);this.setRes
6、izable(false);/ 禁用最大化/ this.init();/ this.setVisible(true);Color c=new Color(255,255,255);ne().setBackground(c.cyan);/* * 初始化计算器 */public void init() /*-组件初始化-*/tf_show = new TextField(0);b_0 = new Button(0);b_point = new Button(.);b_add = new Button(+);b_1 = new Button(1);b_2 = new Button(2);b_3 =
7、new Button(3);b_sub = new Button(-);b_equal = new Button(=);b_4 = new Button(4);b_5 = new Button(5);b_6 = new Button(6);b_mul = new Button(*);b_fra = new Button(1/x);b_7 = new Button(7);b_8 = new Button(8);b_9 = new Button(9);b_div = new Button(/);b_mod = new Button(%);b_back = new Button();b_ce = n
8、ew Button(CE);b_c = new Button(C);b_sign = new Button();b_sqrt= new Button();b_mc = new Button(MC);b_mr = new Button(MR);b_ms = new Button(MS);b_madd = new Button(M+);b_msub = new Button(M-);tf_show.setSelectionStart(tf_show.getText().length();/ 光标放到0的右边/*-组件自定义大小位置-*/tf_show.setBounds(10, 25, 215,
9、50);b_0.setBounds(20, 250, 75, 25);b_point.setBounds(100, 250, 35, 25);b_add.setBounds(140, 250, 35, 25);Bounds(20, 220, 35, 25);b_2.setBounds(60, 220, 35, 25);b_3.setBounds(100, 220, 35, 25);b_sub.setBounds(140, 220, 35, 25);b_equal.setBounds(180, 220, 35, 55);b_4.setBounds(20, 190, 35, 25);b_5.set
10、Bounds(60, 190, 35, 25);b_6.setBounds(100, 190, 35, 25);b_mul.setBounds(140, 190, 35, 25);b_fra.setBounds(180, 190, 35, 25);b_7.setBounds(20, 160, 35, 25);b_8.setBounds(60, 160, 35, 25);b_9.setBounds(100, 160, 35, 25);b_div.setBounds(140, 160, 35, 25);b_mod.setBounds(180, 160, 35, 25);b_back.setBoun
11、ds(20, 130, 35, 25);b_ce.setBounds(60, 130, 35, 25);b_c.setBounds(100, 130, 35, 25);b_sign.setBounds(140, 130, 35, 25);b_sqrt.setBounds(180, 130, 35, 25);b_mc.setBounds(20, 100, 35, 25);b_mr.setBounds(60, 100, 35, 25);b_ms.setBounds(100, 100, 35, 25);b_madd.setBounds(140, 100, 35, 25);b_msub.setBoun
12、ds(180, 100, 35, 25);/*-组件属性设置-*/b_0.setForeground(C_BUTTON);/ 加前景色b_point.setForeground(C_BUTTON);b_add.setForeground(C_BUTTON);b_1.setForeground(C_BUTTON);b_2.setForeground(C_BUTTON);b_3.setForeground(C_BUTTON);b_sub.setForeground(C_BUTTON);b_equal.setForeground(C_BUTTON);b_4.setForeground(C_BUTTO
13、N);b_5.setForeground(C_BUTTON);b_6.setForeground(C_BUTTON);b_mul.setForeground(C_BUTTON);b_fra.setForeground(C_BUTTON);b_7.setForeground(C_BUTTON);b_8.setForeground(C_BUTTON);b_9.setForeground(C_BUTTON);b_div.setForeground(C_BUTTON);b_mod.setForeground(C_BUTTON);b_back.setForeground(C_BUTTON);b_ce.s
14、etForeground(C_BUTTON);b_c.setForeground(C_BUTTON);b_sign.setForeground(C_BUTTON);b_sqrt.setForeground(C_BUTTON);b_mc.setForeground(C_BUTTON);b_mr.setForeground(C_BUTTON);b_ms.setForeground(C_BUTTON);b_madd.setForeground(C_BUTTON);b_msub.setForeground(C_BUTTON);tf_show.setFont(f_show);tf_show.setFor
15、eground(C_BUTTON);/*-设置组件的事件监听-*/b_1.addActionListener(new ActionLin();/ addActionListener是个接口,不能newb_2.addActionListener(new ActionLin();b_3.addActionListener(new ActionLin();b_sub.addActionListener(new OptListener();b_equal.addActionListener(new equalListener();b_4.addActionListener(new ActionLin(
16、);b_5.addActionListener(new ActionLin();b_6.addActionListener(new ActionLin();b_mul.addActionListener(new OptListener();b_fra.addActionListener(new OptListener();b_7.addActionListener(new ActionLin();b_8.addActionListener(new ActionLin();b_9.addActionListener(new ActionLin();b_div.addActionListener(
17、new OptListener();b_mod.addActionListener(new OptListener();b_back.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String str = tf_show.getText();if (!str.equals(0) tf_show.setText(str.substring(0,str.length()-1); elsetf_show.setText(0););b_ce.addActionListener(new
18、OptListener();b_c.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) tf_show.setText(0););/*b_sign.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String str = tf_show.getText();for(;i0;)if(i%2!=0)tf_show.setText(-+str);elseString str1
19、 = str.substring(1, str.length();tf_show.setText(str1);i+;break;);*/b_sign.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) String str=tf_show.getText();if(str.indexOf(-)=-1)str=-+str;elsestr=str.substring(1);tf_show.setText(str););r(new OptListener();b_0.addActionLis
20、tener(new ActionLin();b_point.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / 1-获取文本框的值String str = tf_show.getText();if (str.indexOf(.) = -1) / 2-追加一个点str = str + .;/ 3-重新设置回去tf_show.setText(str););b_add.addActionListener(new OptListener();/*-添加组件-*/this.add(tf_s
21、how);this.add(b_0);this.add(b_point);this.add(b_add);d(b_1);this.add(b_2);this.add(b_3);this.add(b_sub);this.add(b_equal);this.add(b_4);this.add(b_5);this.add(b_6);this.add(b_mul);this.add(b_fra);this.add(b_7);this.add(b_8);this.add(b_9);this.add(b_div);this.add(b_mod);this.add(b_back);this.add(b_ce
22、);this.add(b_c);this.add(b_sign);this.add(b_sqrt);this.add(b_mc);this.add(b_mr);this.add(b_ms);this.add(b_madd);this.add(b_msub);private void show(String num) tf_show.setText(num);/* * 内部类:定义在一个类里的类 1-不能是public 2-内部类相当于本类的一个方法 3-可以访问外部类的成员属性 */ 操作数的事件监听类class ActionLin implements ActionListener publ
23、ic void actionPerformed(ActionEvent e) / 要执行的事/ 获取事件源Button b = (Button) e.getSource();/ getSource()是事件源String num = tf_show.getText();/ 获取事件源上的文本,将文本设置给显示框if (b1 | num.equals(0) / 替换模式tf_show.setText(b.getLabel();b1 = false;/ 将模式改为追加 else String str = tf_show.getText();tf_show.setText(str + b.getLa
24、bel();tf_show.setSelectionStart(tf_show.getText().length();/ 光标放到0的右边/ 操作符的事件监听类class OptListener implements ActionListener public void actionPerformed(ActionEvent e) Button b = (Button) e.getSource();/ getSource()是事件源/* * 1-记录操作符之前先判断在此之前有没有点击过运算符 2-如果有:如此先把前面的运算结果算出来 3-如果没有:如此直接记录 4- * 点过:opt不为nul
25、l */if (null != opt) /* * 1-拿到现在文本框里的数加数 2-进展运算 3-运算完之后将结果显示在文本框里边 */num2 = Double.parseDouble(tf_show.getText();if (opt.equals(+) double num = num1 + num2;tf_show.setText(String.valueOf(num); else if (opt.equals(-) double num = num1 - num2;tf_show.setText(String.valueOf(num); else if (opt.equals(*)
26、 double num = num1 * num2;tf_show.setText(String.valueOf(num); else if (opt.equals(/) double num = num1 / num2;tf_show.setText(String.valueOf(num);/ 记录操作符opt = b.getLabel();/ 记录被加数num1 = Double.parseDouble(tf_show.getText();/ System.out.println(opt);b1 = true;/ 刚点完操作符,将模式改为替换/ 定义等于的事件监听类class equalL
27、istener implements ActionListener public void actionPerformed(ActionEvent e) num2 = Double.parseDouble(tf_show.getText();/ 获取操作符/ 计算结果if (opt.equals(+) double add = num1 + num2;if (add = (int) add) tf_show.setText(String.valueOf(int) add); else tf_show.setText(String.valueOf(add); else if (opt.equal
28、s(-) double sub = num1 - num2;if (sub = (int) sub) tf_show.setText(String.valueOf(int) sub); else tf_show.setText(String.valueOf(sub); else if (opt.equals(*) double mul = num1 * num2;if (mul = (int) mul) tf_show.setText(String.valueOf(int) mul); else tf_show.setText(String.valueOf(mul); else if (opt
29、.equals(/) if (num2 != 0) double div = num1 / num2;if (div = (int) div) tf_show.setText(String.valueOf(int) div); else tf_show.setText(String.valueOf(div); else tf_show.setText(除数不能为0); else if (opt.equals(%) if (num2 != 0) double mod = num1 % num2;tf_show.setText(String.valueOf(mod); else tf_show.setText(除数不能为0);/ 结果已经算出,之前的操作符可以清掉opt = null;b1 = true;/ 将追加改为替换/* * 1-去除 2-退格 3-0 4-. 5-+/- 6- 连+ */TestJsq.Javapackage 练习题;public class TestJsq public static void main(String args) Jsp jsp=new Jsp(计算器);jsp.init();jsp.setVisible(true);11 / 11