用java编写一个简易的计算器代码_第1页
用java编写一个简易的计算器代码_第2页
用java编写一个简易的计算器代码_第3页
用java编写一个简易的计算器代码_第4页
用java编写一个简易的计算器代码_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.eve nt.Act ion Eve nt;import java.awt.eve nt.Act ion Liste ner;import java.awt.eve nt.KeyEve nt;import java.awt.eve nt.KeyListe ner;import javax.swi ng.JButt on;import javax.swi ng.JFrame;import javax.swi n

2、g.JPa nel;import javax.swi ng.JTextField;/* 使用 java 语言开发一个简易计算器*/public class TestJsq exte nds JFrame impleme nts Actio nListe ner, KeyListe ner private JTextField jtf;private JButt on jb_bk, jb_ce, jb_c;private String xs =;private double sum = 0;private int fh;public static void main( Stri ng args)

3、 new TestJsq().creatCUI();public void creatCUI() JFrame jf = new JFrame();jf.setTitle( 计算器 );jtf = new JTextField(0.);jtf.setHorizo ntalAlig nmen t(JTextField.RIGHT);jf.add(jtf, BorderLayout.NORTH);JPa nel jp_ma in = new JPa nel( new BorderLayout(); jf.add(jp_ma in);JPa nel jp1 = new JPanel(new Grid

4、Layout(1,3, 1, 1); jb_bk = new JButto n( Backspace);jb_bk.setForegrou nd(Color.RED); jb_bk.addAct ion Liste ner(this); jb_ce = new JButto n( CE);jb_ce.setForegro un d(Color.RED);jb_ce.addAct ion Liste ner(this);jb_c = new JButto n( C);jb_c.setForegro un d(Color.RED); jb_c.addActi on Liste n er(this)

5、; jp1.add(jb_bk);jp1.add(jb_ce);jp1.add(jb_c);jp_mai n.add(jp1, BorderLayout.NORTH);JPa nel jp2 = new JPa nel( new GridLayout(4, 5,1,1);1 2 3 1/x 0 +/ + 一 String str 一 7, 8, 9, /, sqrt, 4, 5, 6, *, %,for (i nt i=:0; i 20; i+) JButton jb = new JButt on (stri);jb.addAct ion Liste n er(this);jb.setFore

6、gro un d(Color.blue);if (i 一一 3 | i 一一 8 | i 一一 13 | i 一一 18 | i 一一 19) jb.setForegro un d(Color.RED);jp2.add(jb);jp_mai n.add(jp2, BorderLayout.CENTER);jf.setResizable(false);jf.setBou nds(50, 50, 350, 350);jf.setVisible(true);Overridepublic void acti on Performed(Acti on Eve nt e) Stri ng s 一 e.ge

7、tAct ion Comma n d(); if (s.equals(CE) | s.equals(C) jtf.setText(0.); else if (s.equals(Backspace) Stri ng str 一 xs.substri n g(0, xsen gth() - 1); xs 一 str; jtf.setText(xs); else if (s.equals(7) | s.equals(8) | s.equals(9)| s.equals(4) | s.equals(5) | s.equals(6)| s.equals(1) | s.equals(2) | s.equa

8、ls(3)| s.equals(0) | s.equals(.) xs 一 xs + s;jtf.setText(xs); else if (s.equals(+/-) double x = Double.parseDouble(xs); xs= + (-x); jtf.setText(xs); else if (s.equals(/) fh = 4;int point = xs.i ndexOf(.);if (poi nt != -1) String strt = xs.substri ng(0, poin t);String z = xs.substri ng(po int + 1, xs

9、.len gth(); int z_num = z.len gth();sum = In teger.decode(strt) + (double) (In teger.decode(z) / (10 * z_n um); else sum = In teger.decode(xs); xs =; jtf.setText( null); else if (s.equals(*) fh = 3;int point = xs.i ndexOf(.);if (poi nt != -1) String strt = xs.substri ng(0, poin t);String z = xs.subs

10、tri ng(po int + 1, xs.len gth(); int z_num = z.len gth();sum = In teger.decode(strt) + (double) (In teger.decode(z) / (10 * z_nu m); else sum = In teger.decode(xs); xs =; jtf.setText( null); else if (s.equals(-) fh = 2;int point = xs.i ndexOf(.);if (poi nt != -1) String strt = xs.substri ng(0, poin

11、t);String z = xs.substri ng(po int + 1, xs.len gth(); int z_num = z.len gth();sum = In teger.decode(strt) + (double) (In teger.decode(z) / (10 * z_nu m); else sum = In teger.decode(xs);xs =; jtf.setText( null); else if (s.equals(+) fh = 1;int point = xs.i ndexOf(.);if (poi nt != -1) String strt = xs

12、.substri ng(0, poin t);String z = xs.substri ng(po int + 1, xs.len gth(); int z_num = z.len gth();sum = In teger.decode(strt) + (double) (In teger.decode(z) / (10 * z_nu m); else sum = In teger.decode(xs);xs =; jtf.setText( null); else if (s.equals(sqrt) double x = Double.parseDouble(xs);double x1 =

13、 Math.sqrt(x);xs = x1 + ;jtf.setText(xs); else if (s.equals(%) double x = Double.parseDouble(xs); x=0.01 * x*x;xs=x+; jtf.setText(xs); else if (s.equals(1/x) if (xs = 0) jtf.setText ( ”除数不能为 0); else double x = Double.parseDouble(xs);double x1 = 1 / x;xs = x1 + ;jtf.setText(Stri ng.valueOf(xs); else

14、 if (s.equals(=) if (fh != 0) switch (fh) case 1:in t poi nt1 = xs.i ndexOf(.);if (poi nt1 != -1) String s1 = xs.substri ng(0, poin t1);String z1 = xs.substri ng(po int1 + 1, xs.len gth();int z1_num = z1.len gth();xs =;double ss1 = In teger.decode(s1)+ (double) In teger.decode(z1) / (10 * z1_nu m);

15、sum = sum + ss1; else sum = sum + In teger.decode(xs); xs =;fh = 0;break;case 2:in t poi nt2 = xs.i ndexOf(.);if (poi nt2 != -1) String s2 = xs.substri ng(0, poin t2);String z2 = xs.substri ng(po int2 + 1, xs.len gth();int z2_num = z2.len gth();xs =;double ss2 = In teger.decode(s2)+ (double) In tege

16、r.decode(z2) / (10 * z2_num); sum = sum - ss2; else sum = sum - In teger.decode(xs); xs =;fh = 0;break;case 3:in t poi nt3 = xs.i ndexOf(.);if (poi nt3 != -1) String s3 = xs.substri ng(0, poin t3);String z3 = xs.substri ng(po int3 + 1, xs.len gth(); int z1_num = z3.len gth();xs =;double s4 = In tege

17、r.decode(s3)+ (double) In teger.decode(z3) / (10 * z1_num); sum = sum * s4; else sum = sum * In teger.decode(xs); xs =; fh = 0; break;case 4:in t poi nt4 = xs.i ndexOf(.);if (poi nt4 != -1) String s4 = xs.substri n g(0, poin t4);String z4 = xs.substri n g(po int4 + 1, xsen gth(); i nt z4_num = z4en gth();xs =;double s5 =ln teger.decode(s4)+ (double) In teger.decode(z4) / (10 * z4_nu m); sum = sum / s5; else sum = sum / In teger.decode(xs); xs =;fh = 0;break;default:break;xs = Stri n g.valueOf(sum); jtf.se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论