java设计计算器源代码_第1页
java设计计算器源代码_第2页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

1、 16/16java设计计算器源代码 import javax.swing.*; importstatic javax.swing.JFrame.*; /引入JFrame的静态常量 import java.awt.event.*; import java.awt.*; enum Oper加,减,乘,除,取反,开方,百分号,倒数,对数,正弦,余弦,取余,平方 class MyExtendsJFrame extends JFrame implements ActionListener,KeyListener JTextField text;/创建文本框对象 JButton button1, but

2、ton2,button3; JButton button_number1,button_number2,button_number3,button_number4,button_nu mber5,button_number6,button_number7,button_number8,button_number9,but ton_number0,button_number10; JButton button_add,button_equ,button_sub,button_mul,button_div,button_per,but ton_pec,button_sqrt,button_not,

3、button_log,button_cos,button_sin,butto n_mod,button_squ; JMenuBar menubar; JMenu menu1,menu2,menu3; JMenuItem menuitem1,menuitem2,menuitem3,menuitem4,menuitem5,menuitem6,menuitem7 ; double CurrentNumber; Oper oper; boolean A=false; publicvoid keyPressed(KeyEvent e) publicvoid keyReleased (KeyEvent e

4、) if(e.getKeyCode()!=KeyEvent.VK_0 text.setText(); for (int i = 0; i s.length() - 1; i+) char a = s.charAt(i); text.setText(text.getText() + a); publicvoid keyTyped(KeyEvent e) public MyExtendsJFrame() setTitle(计算器); setBounds(160,100,315,455); setLayout(null); init(); /添加控件的操作封装成一个函数 setVisible(tru

5、e);/放在添加组件后面执行 setDefaultCloseOperation(EXIT_ON_CLOSE); void init()/添加的控件 text=new JTextField(20); text.setBounds(20,10,260,50);/设置大小和位置 add(text);/添加到窗口 / 当点击按钮+、-、*、/时,com = true text.addKeyListener(this); menubar=new JMenuBar(); setJMenuBar(menubar); menu1=new JMenu(查看(E); menu1.setFont(new Font(

6、粗体,Font.PLAIN,12); menu2=new JMenu(编辑(v); menu2.setFont(new Font(粗体,Font.PLAIN,12); menu3=new JMenu(帮助); menu3.setFont(new Font(粗体,Font.PLAIN,12); menubar.add(menu1);menubar.add(menu2);menubar.add(menu3); menuitem1=new JMenuItem(标准型(T); menuitem2=new JMenuItem(科学型(S); menuitem3=new JMenuItem(数学分组(L)

7、; menuitem1.setFont(new Font(粗体,Font.PLAIN,12); menuitem2.setFont(new Font(粗体,Font.PLAIN,12); menuitem3.setFont(new Font(粗体,Font.PLAIN,12); menu1.add(menuitem1);menu1.add(menuitem2);menu1.addSeparator();me nu1.add(menuitem3); menuitem4=new JMenuItem(复制(c) Ctrl+c); menuitem5=new JMenuItem(粘贴(v) Ctrl+

8、v); menuitem4.setFont(new Font(粗体,Font.PLAIN,12); menuitem5.setFont(new Font(粗体,Font.PLAIN,12); menu2.add(menuitem4);menu2.add(menuitem5); menuitem6=new JMenuItem(查看帮助(H); menuitem7=new JMenuItem(关于计算器(A); menuitem6.setFont(new Font(粗体,Font.PLAIN,12); menuitem7.setFont(new Font(粗体,Font.PLAIN,12); me

9、nu3.add(menuitem6);menu3.addSeparator(); menu3.add(menuitem7); button1=new JButton(); button1.setBounds(20,70,52,52); add(button1); button1.addActionListener(this); button2=new JButton(CE); button2.setBounds(72,70,52,52); add(button2); button2.addActionListener(this); button3=new JButton(C); button3

10、.setBounds(124,70,52,52); add(button3); button3.addActionListener(this); button_number1=new JButton(1); button_number1.setBounds(20,278,52,52); add(button_number1); button_number1.addActionListener(this);/添加关联 button_number2=new JButton(2); button_number2.setBounds(72,278,52,52); add(button_number2)

11、; button_number2.addActionListener(this);/添加关联 button_number3=new JButton(3); button_number3.setBounds(124,278,52,52); add(button_number3); button_number3.addActionListener(this);/添加关联 button_number4=new JButton(4); button_number4.setBounds(20,226,52,52); add(button_number4); button_number4.addActio

12、nListener(this);/添加关联 button_number5=new JButton(5); button_number5.setBounds(72,226,52,52); add(button_number5); button_number5.addActionListener(this);/添加关联 button_number6=new JButton(6); button_number6.setBounds(124,226,52,52); add(button_number6); button_number6.addActionListener(this);/添加关联 but

13、ton_number7=new JButton(7); button_number7.setBounds(20,174,52,52); add(button_number7); button_number7.addActionListener(this);/添加关联 button_number8=new JButton(8); button_number8.setBounds(72,174,52,52); add(button_number8); button_number8.addActionListener(this);/添加关联 button_number9=new JButton(9)

14、; button_number9.setBounds(124,174,52,52); add(button_number9); button_number9.addActionListener(this);/添加关联 button_number0=new JButton(0); button_number0.setBounds(20,330,104,52); add(button_number0); button_number0.addActionListener(this);/添加关联 button_number10=new JButton(.); button_number10.setBo

15、unds(124,330,52,52); add(button_number10); button_number10.addActionListener(this);/添加关联 button_equ=new JButton(=); button_equ.setBounds(228,278,52,104); add(button_equ); button_equ.addActionListener(this);/添加关联 button_add=new JButton(+); button_add.setBounds(176,330,52,52); add(button_add); button_

16、add.addActionListener(this);/添加关联 button_sub=new JButton(-); button_sub.setBounds(176,278,52,52); button_sub.addActionListener(this); button_mul=new JButton(*); button_mul.setBounds(176,226,52,52); add(button_mul); button_mul.addActionListener(this); button_div=new JButton(/); button_div.setBounds(1

17、76,174,52,52); add(button_div); button_div.addActionListener(this); button_sqrt=new JButton(); button_sqrt.setBounds(228,70,52,52); add(button_sqrt); button_sqrt.addActionListener(this); button_not=new JButton(-+); button_not.setBounds(176,70,52,52); add(button_not); button_not.addActionListener(thi

18、s); button_per=new JButton(%); button_per.setBounds(228,174,52,52); add(button_per); button_per.addActionListener(this); button_pec=new JButton(1/x); button_pec.setBounds(228,226,52,52); add(button_pec); button_pec.addActionListener(this); button_log=new JButton(log); button_log.setBounds(20,122,52,

19、52); add(button_log); button_log.addActionListener(this); button_cos=new JButton(cos); button_cos.setBounds(72,122,52,52); add(button_cos); button_cos.addActionListener(this); button_sin=new JButton(sin); button_sin.setBounds(124,122,52,52); button_sin.addActionListener(this); button_mod=new JButton

20、(mod); button_mod.setBounds(176,122,52,52); add(button_mod); button_mod.addActionListener(this); button_squ=new JButton(x2); button_squ.setBounds(228,122,52,52); add(button_squ); button_squ.addActionListener(this); button_number1.setBackground(new Color(176,196,222);button_number1.setFont(new Font(宋

21、体,0,25);button_number1.setForeground(Color.white); button_number2.setBackground(new Color(176,196,222);button_number2.setFont(new Font(宋体,0,25);button_number2.setForeground(Color.white); button_number3.setBackground(new Color(176,196,222);button_number3.setFont(new Font(宋体,0,25);button_number3.setFo

22、reground(Color.white); button_number4.setBackground(new Color(176,196,222);button_number4.setFont(new Font(宋体,0,25);button_number4.setForeground(Color.white); button_number5.setBackground(new Color(176,196,222);button_number5.setFont(new Font(宋体,0,25);button_number5.setForeground(Color.white); butto

23、n_number6.setBackground(new Color(176,196,222);button_number6.setFont(new Font(宋体,0,25);button_number6.setForeground(Color.white); button_number7.setBackground(new Color(176,196,222);button_number7.setFont(new Font(宋体,0,25);button_number7.setForeground(Color.white); button_number8.setBackground(new

24、Color(176,196,222);button_number8.setFont(new Font(宋体,0,25);button_number8.setForeground(Color.white); button_number9.setBackground(new Color(176,196,222);button_number9.setFont(new Font(宋体,0,25);button_number9.setForeground(Color.white); button_number0.setBackground(new Color(176,196,222);button_nu

25、mber0.setFont(new Font(宋体,0,25);button_number0.setForeground(Color.white); button_number10.setBackground(new Color(176,196,222);button_number10.setFont(new Font(宋体 ,0,25);button_number10.setForeground(Color.white); button1.setBackground(new Color(119,136,153);button1.setFont(new Font(宋体,0,18);button

26、1.setForeground(Color.white); button2.setBackground(new Color(119,136,153);button2.setFont(new Font(宋体,0,18);button2.setForeground(Color.white); button3.setBackground(new Color(119,136,153);button3.setFont(new Font(宋体,0,18);button3.setForeground(Color.white); button_add.setBackground(new Color(119,1

27、36,153);button_add.setFont(new Font(宋体 ,0,18);button_add.setForeground(Color.white); button_equ.setBackground(new Color(119,136,153);button_equ.setFont(new Font(宋体 ,0,18);button_equ.setForeground(Color.white); button_sub.setBackground(new Color(119,136,153);button_sub.setFont(new Font(宋体 ,0,18);butt

28、on_sub.setForeground(Color.white); button_mul.setBackground(new Color(119,136,153);button_mul.setFont(new Font(宋体 ,0,18);button_mul.setForeground(Color.white); button_div.setBackground(new Color(119,136,153);button_div.setFont(new Font(宋体 ,0,18);button_div.setForeground(Color.white); button_per.setB

29、ackground(new Color(119,136,153);button_per.setFont(new Font(宋体 ,0,18);button_per.setForeground(Color.white); button_pec.setBackground(new Color(119,136,153);button_pec.setFont(new Font(宋体 ,0,12);button_pec.setForeground(Color.white); button_sqrt.setBackground(new Color(119,136,153);button_sqrt.setF

30、ont(new Font(宋体 ,0,18);button_sqrt.setForeground(Color.white); button_not.setBackground(new Color(119,136,153);button_not.setFont(new Font(宋体 ,0,18);button_not.setForeground(Color.white); button_log.setBackground(new Color(119,136,153);button_log.setFont(new Font(宋体 ,0,12);button_log.setForeground(C

31、olor.white); button_cos.setBackground(new Color(119,136,153);button_cos.setFont(new Font(宋体 ,0,12);button_cos.setForeground(Color.white); button_sin.setBackground(new Color(119,136,153);button_sin.setFont(new Font(宋体 ,0,12);button_sin.setForeground(Color.white); button_mod.setBackground(new Color(11

32、9,136,153);button_mod.setFont(new Font(宋体 ,0,12);button_mod.setForeground(Color.white); button_squ.setBackground(new Color(119,136,153);button_squ.setFont(new Font(宋体,0,12);button_squ.setForeground(Color.white); class testException extends Exception String message; testException() message =除数等于0; pu

33、blic String getMessage() return message; class testException2extends Exception String message; testException2() message =被开方数小于0; public String getMessage() return message; class testException3extends Exception String message; testException3() message =对数小于等于0; public String getMessage() return mess

34、age; class testf void f(double i) throws testException if(i=0) thrownew testException(); class testf2 void f2(double i) throws testException2 if(i0) thrownew testException2(); class testf3 void f3(double i) throws testException3 if(i=0) thrownew testException3(); publicvoid actionPerformed(ActionEve

35、nt e) if(e.getSource()=button_number1) if(A) text.setText(1); A=false; else if(text.getText().equals(0) text.setText(1); else text.setText(text.getText()+1); if(e.getSource()=button_number2) if(A) text.setText(2); A=false; else if(text.getText().equals(0) text.setText(2); else text.setText(text.getT

36、ext()+2); if(e.getSource()=button_number3) if(A) text.setText(3); A=false; else if(text.getText().equals(0) text.setText(3); else text.setText(text.getText()+3); if(e.getSource()=button_number4) if(A) text.setText(4); A=false; else if(text.getText().equals(0) text.setText(4); else text.setText(text.

37、getText()+4); if(e.getSource()=button_number5) if(A) text.setText(5); A=false; else if(text.getText().equals(0) text.setText(5); else text.setText(text.getText()+5); if(e.getSource()=button_number6) if(A) text.setText(6); A=false; else if(text.getText().equals(0) text.setText(6); else text.setText(t

38、ext.getText()+6); if(e.getSource()=button_number7) if(A) text.setText(7); A=false; else if(text.getText().equals(0) text.setText(7); else text.setText(text.getText()+7); if(e.getSource()=button_number8) if(A) text.setText(8); A=false; else if(text.getText().equals(0) text.setText(8); else text.setTe

39、xt(text.getText()+8); if(e.getSource()=button_number9) if(A) text.setText(9); A=false; else if(text.getText().equals(0) text.setText(9); else text.setText(text.getText()+9); if(e.getSource()=button_number0) if(A) text.setText(0); A=false; else if (text.getText().equals(0) A=false; else text.setText(

40、text.getText()+0); if(e.getSource()=button_number10) if(A) text.setText(0.); A=false; else if (text.getText().indexOf(.) != -1) else if (text.getText().equals() text.setText(0.); A=false; else text.setText(text.getText()+.); if(e.getSource()=button1) String s = text.getText(); text.setText(); for (i

41、nt i = 0; i s.length() - 1; i+) char a = s.charAt(i); text.setText(text.getText() + a); if(e.getSource()=button2) text.setText(); if(e.getSource()=button3) text.setText(); if(e.getSource()=button_add) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.加; text.setText(); if(e.getSource()=butt

42、on_sub) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.减; text.setText(); if(e.getSource()=button_mul) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.乘; text.setText(); if(e.getSource()=button_div) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.除; text.setText();

43、if(e.getSource()=button_per) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.百分号; text.setText(); if(e.getSource()=button_pec) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.倒数; text.setText(); if(e.getSource()=button_sqrt) CurrentNumber=Double.parseDouble(text.getText(); oper

44、=Oper.开方; text.setText(); if(e.getSource()=button_not) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.取反; text.setText(); if(e.getSource()=button_log) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.对数; text.setText(); if(e.getSource()=button_sin) CurrentNumber=Double.parseDou

45、ble(text.getText(); oper=Oper.正弦; text.setText(); if(e.getSource()=button_cos) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.余弦; text.setText(); if(e.getSource()=button_mod) CurrentNumber=Double.parseDouble(text.getText(); oper=Oper.取余; text.setText(); if(e.getSource()=button_squ) Curre

46、ntNumber=Double.parseDouble(text.getText(); oper=Oper.平方; text.setText(); if(e.getSource()=button_equ) A=true; switch(oper) case加: double number1=Double.parseDouble(text.getText()+CurrentNumber; text.setText(String.valueOf(number1); break; case减: double number2=CurrentNumber-Double.parseDouble(text.

47、getText(); text.setText(String.valueOf(number2); break; case乘: double number3=Double.parseDouble(text.getText()*CurrentNumber; text.setText(String.valueOf(number3); break; case除: if(Double.parseDouble(text.getText()=0) testf t=new testf(); try t.f(Double.parseDouble(text.getText(); catch(testException e1) System.out.println(e1.getMessage(); text.setText(e1.getMessa

温馨提示

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

评论

0/150

提交评论