java编写的简单的计算器程序_第1页
java编写的简单的计算器程序_第2页
java编写的简单的计算器程序_第3页
java编写的简单的计算器程序_第4页
java编写的简单的计算器程序_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

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

文档简介

1、精选优质文档-倾情为你奉上计算器项目内容:编写一个Applet,模仿windows附件所带计算器的功能,可以帮助用户完成计算功能,具体如下图所示。项目要求:使用图形的方式借助窗口、菜单、按钮等标准界面元素和鼠标操作,来帮助用户方便地向计算机系统发出命令,启动操作,并将系统运行的结果同样以图形的方式显示给用户,这样更加直观和生动; 1. Applet容器中组件的添加与设置,包括面板以及菜单的使用; 2. 容器中组件的布局管理; 3. Java核心包中数组、数学计算类的使用; 4. 异常的处理; 5. 事件处理模型中的三类对象的使用: 1. Event-事件,用户对界面操作在java语言上的描述,

2、以类的形式出现,例如键盘操作对应的事件类是KeyEvent。 2. Event Source-事件源,事件发生的场所,通常就是各个组件,例如按钮Button。 3. Event handler-事件处理者,接收事件对象并对其进行处理的对象。 6. 程序中事件处理的流程:1. 计算流程的细化 参考代码:import java.awt.*;import java.lang.*;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.text.DecimalFormat;public cl

3、ass Calculator    implements ActionListener /导入动作监听接口  /设计面板中的单位  JFrame frame;  JTextField textAnswer;  JPanel panel, panel1, panel2, panel3;  JMenuBar mainMenu;  JTextField textMemory;  JLabel labelMemSpace; /labelMemSpace单纯做摆设,控制面板的形状  JButto

4、n buttonBk, buttonCe, buttonC;  JButton button;  JButton buttonMC, buttonMR, buttonMS, buttonMAdd;  JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,      buttonDiv, buttonMod;  JButton buttonSqrt, buttonDao, buttonEqual;  JMenu ed

5、itMenu, viewMenu, helpMenu;  JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal;  DecimalFormat df; /设置数据输出精度  boolean clickable; /控制当前能否按键  double memoryd; /使用内存中存储的数字  int memoryi;  double vard, answerd; /用来保存double型数据的中间值(vard)和最后结果(answerd)&

6、#160; short key = -1, prekey = -1; /key用来保存当前进行何种运算,prekey用来保存前次进行何种运算  String copy; /做复制用  JTextArea help; /帮助  JScrollPane scrollHelp;  /构造函数  public Calculator()     clickable = true;    answerd = 0;    frame = new JFrame("

7、;计算器");    df = new DecimalFormat("0.#"); /设置数据输出精度(对于double型值)    textAnswer = new JTextField(15);    textAnswer.setText("");    textAnswer.setEditable(false);    textAnswer.setBackground(new Color(2

8、55, 255, 255);    panel = new JPanel();    frame.getContentPane().add(panel);    panel1 = new JPanel();    panel2 = new JPanel();    panel.setLayout(new BorderLayout();    /设计整个面板    mainMenu = n

9、ew JMenuBar();    editMenu = new JMenu("编辑(E)");    viewMenu = new JMenu("查看(V)");    helpMenu = new JMenu("帮助(H)");    copyItem = new JMenuItem("   复制(C) Ctrl+C");    copyIte

10、m.addActionListener(this);    pasteItem = new JMenuItem("   粘贴(V) Ctrl+V");    pasteItem.addActionListener(this);    editMenu.add(copyItem);    editMenu.add(pasteItem);    tItem = new JMenuItem("标准型(T

11、)");    tItem.addActionListener(this);    sItem = new JMenuItem("   科学型(S)");    sItem.addActionListener(this);    numberGroup = new JMenuItem("   数字分组(I)");    numberGroup.addActi

12、onListener(this);    viewMenu.add(tItem);    viewMenu.add(sItem);    viewMenu.add(numberGroup);    topHelp = new JMenuItem("   帮助主题(H)");    topHelp.addActionListener(this);    help = new JT

13、extArea(5, 20);    scrollHelp = new JScrollPane(help);    help.setEditable(false);    help.append("执行简单计算n");    help.append("1.  键入计算的第一个数字。n");    help.append("2.  单击“+”执行加、“-”执行减、“*”执行乘或“

14、/”执行除。n");    help.append("3.  键入计算的下一个数字。n");    help.append("4.  输入所有剩余的运算符和数字。n");    help.append("5.  单击“=”。n");    aboutCal = new JMenuItem("   关于计算器(A)"); 

15、0;  aboutCal.addActionListener(this);    helpMenu.add(topHelp);    helpMenu.add(aboutCal);    mainMenu.add(editMenu);    mainMenu.add(viewMenu);    mainMenu.add(helpMenu);    panel.add(mainMenu, BorderLayo

16、ut.NORTH);    panel.add(textAnswer, BorderLayout.CENTER);    panel.add(panel1, BorderLayout.SOUTH);    panel1.setLayout(new BorderLayout();    textMemory = new JTextField(3);    textMemory.setEditable(false);   

17、; textMemory.setBackground(new Color(217, 217, 217);    labelMemSpace = new JLabel("                   ");    buttonBk = new JButton("Backspace");  &#

18、160; buttonBk.setForeground(new Color(255, 0, 0);    buttonCe = new JButton("CE");    buttonCe.setForeground(new Color(255, 0, 0);    buttonC = new JButton("C");    buttonC.setForeground(new Color(255, 0, 0);  

19、  buttonBk.addActionListener(this);    buttonCe.addActionListener(this);    buttonC.addActionListener(this);    panel1.add(panel2, BorderLayout.NORTH);    panel2.setLayout(new FlowLayout(FlowLayout.RIGHT);    panel2.add(

20、textMemory);    panel2.add(labelMemSpace);    panel2.add(buttonBk);    panel2.add(buttonCe);    panel2.add(buttonC);    panel3 = new JPanel();    panel1.add(panel3, BorderLayout.CENTER);    butto

21、n = new JButton10;    for (int i = 0; i < button.length; i+)       buttoni = new JButton(Integer.toString(i);      buttoni.setForeground(new Color(0, 0, 255);        buttonMC = new JButton("MC"

22、;);    buttonMC.setForeground(new Color(255, 0, 0);    buttonMR = new JButton("MR");    buttonMR.setForeground(new Color(255, 0, 0);    buttonMS = new JButton("MS");    buttonMS.setForeground(new Color(255

23、, 0, 0);    buttonMAdd = new JButton("M+");    buttonMAdd.setForeground(new Color(255, 0, 0);    buttonDot = new JButton(".");    buttonDot.setForeground(new Color(0, 0, 255);    buttonAddAndSub = new JBut

24、ton("+/-");    buttonAddAndSub.setForeground(new Color(0, 0, 255);    buttonAdd = new JButton("+");    buttonAdd.setForeground(new Color(255, 0, 0);    buttonSub = new JButton("-");    buttonSub.

25、setForeground(new Color(255, 0, 0);    buttonMul = new JButton("*");    buttonMul.setForeground(new Color(255, 0, 0);    buttonDiv = new JButton("/");    buttonDiv.setForeground(new Color(255, 0, 0);    bu

26、ttonMod = new JButton("%");    buttonMod.setForeground(new Color(0, 0, 255);    buttonSqrt = new JButton("sqrt");    buttonSqrt.setForeground(new Color(0, 0, 255);    buttonDao = new JButton("1/x");  

27、  buttonDao.setForeground(new Color(0, 0, 255);    buttonEqual = new JButton("=");    buttonEqual.setForeground(new Color(255, 0, 0);    /将所有行为与监听绑定    panel3.setLayout(new GridLayout(4, 6);    panel3.add(butto

28、nMC);    buttonMC.addActionListener(this);    panel3.add(button7);    button7.addActionListener(this);    panel3.add(button8);    button8.addActionListener(this);    panel3.add(button9);    butto

29、n9.addActionListener(this);    panel3.add(buttonDiv);    buttonDiv.addActionListener(this);    panel3.add(buttonSqrt);    buttonSqrt.addActionListener(this);    panel3.add(buttonMR);    buttonMR.addActionListen

30、er(this);    panel3.add(button4);    button4.addActionListener(this);    panel3.add(button5);    button5.addActionListener(this);    panel3.add(button6);    button6.addActionListener(this);    pa

31、nel3.add(buttonMul);    buttonMul.addActionListener(this);    panel3.add(buttonMod);    buttonMod.addActionListener(this);    panel3.add(buttonMS);    buttonMS.addActionListener(this);    panel3.add(button1);&#

32、160;   button1.addActionListener(this);    panel3.add(button2);    button2.addActionListener(this);    panel3.add(button3);    button3.addActionListener(this);    panel3.add(buttonSub);    buttonSub.a

33、ddActionListener(this);    panel3.add(buttonDao);    buttonDao.addActionListener(this);    panel3.add(buttonMAdd);    buttonMAdd.addActionListener(this);    panel3.add(button0);    button0.addActionListener(thi

34、s);    panel3.add(buttonAddAndSub);    buttonAddAndSub.addActionListener(this);    panel3.add(buttonDot);    buttonDot.addActionListener(this);    panel3.add(buttonAdd);    buttonAdd.addActionListener(this);

35、60;   panel3.add(buttonEqual);    buttonEqual.addActionListener(this);    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);    frame.pack();    frame.show();    /设置各个按钮行为  public void actionPerformed(ActionEvent

36、event)     boolean sign = false; /判断是否是double型数参与运算,是为true,不是为false    Object temp = event.getSource();    try       /如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后      for (int i = 0; i <= 9; i+)   

37、     if (temp = buttoni && clickable = true)          textAnswer.setText(textAnswer.getText() + Integer.toString(i);          /按下'.'按钮时,判断当前文本框内字符串中含不含'.',如果已含,则不允许再插

38、入'.'      if (temp = buttonDot && clickable = true)         boolean isDot = false;        if (textAnswer.getText().length() = 0)          isDot

39、= true;        for (int i = 0; i < textAnswer.getText().length(); i+)          if ('.' = textAnswer.getText().charAt(i)             isDot = true; 

40、60;          break;                  if (isDot = false)          textAnswer.setText(textAnswer.getText() + "."); &#

41、160;          if ( (temp = buttonAdd | temp = buttonSub | temp = buttonMul |            temp = buttonDiv) && clickable = true)         /'+'操作  &

42、#160;     if (temp = buttonAdd)           switch (prekey)             case 0:              answerd += Double.parseD

43、ouble(textAnswer.getText();              break;            case 1:              answerd -= Double.parseDouble(textA

44、nswer.getText();              break;            case 2:              answerd *= Double.parseDouble(textAnswer.getTe

45、xt();              break;            case 3:              if (Double.parseDouble(textAnswer.getText() = 0)  &#

46、160;              textAnswer.setText("除数不能为零");                clickable = false;           

47、60;                else                answerd /= Double.parseDouble(textAnswer.getText();           

48、;   break;            default:              answerd = Double.parseDouble(textAnswer.getText();             

49、60;      textAnswer.setText("");          prekey = key = 0;                /'-'操作        if (temp = buttonS

50、ub)           switch (prekey)             case 0:              answerd += Double.parseDouble(textAnswer.getText();   

51、60;          break;            case 1:              answerd -= Double.parseDouble(textAnswer.getText();     &#

52、160;        break;            case 2:              answerd *= Double.parseDouble(textAnswer.getText();       &

53、#160;      break;            case 3:              if (Double.parseDouble(textAnswer.getText() = 0)          &#

54、160;      textAnswer.setText("除数不能为零");                clickable = false;                    &#

55、160;       else                answerd /= Double.parseDouble(textAnswer.getText();              break;    

56、0;       default:              answerd = Double.parseDouble(textAnswer.getText();                    textAnswer.set

57、Text("");          prekey = key = 1;                /'*'操作        if (temp = buttonMul)        &#

58、160;  switch (prekey)             case 0:              answerd += Double.parseDouble(textAnswer.getText();           

59、60;  break;            case 1:              answerd -= Double.parseDouble(textAnswer.getText();              b

60、reak;            case 2:              answerd *= Double.parseDouble(textAnswer.getText();              break; 

61、           case 3:              if (Double.parseDouble(textAnswer.getText() = 0)                 textAnswer.se

62、tText("除数不能为零");                clickable = false;                            else

63、60;               answerd /= Double.parseDouble(textAnswer.getText();              break;            default:&

64、#160;             answerd = Double.parseDouble(textAnswer.getText();                    textAnswer.setText("");     

65、;     prekey = key = 2;                /'/'操作        if (temp = buttonDiv)           switch (prekey)    

66、60;        case 0:              answerd += Double.parseDouble(textAnswer.getText();              break;     &#

67、160;      case 1:              answerd -= Double.parseDouble(textAnswer.getText();              break;       &

68、#160;    case 2:              answerd *= Double.parseDouble(textAnswer.getText();              break;         

69、   case 3:              if (Double.parseDouble(textAnswer.getText() = 0)                 textAnswer.setText("除数不能为零");   

70、0;            clickable = false;                            else        

71、60;       answerd /= Double.parseDouble(textAnswer.getText();              break;            default:        &

72、#160;     answerd = Double.parseDouble(textAnswer.getText();                    textAnswer.setText("");          prekey = key = 3; 

73、;                   /'='操作      if (temp = buttonEqual && clickable = true)         /如果连续按'=',则进行连续运算      

74、;  if (prekey = 5)           if (key = 0)             answerd += vard;            textAnswer.setText(df.format(answerd);   &#

75、160;                if (key = 1)             answerd -= vard;            textAnswer.setText(df.format(answerd); 

76、60;                  if (key = 2)             answerd *= vard;            textAnswer.setText(df.format(answerd

77、);                    if (key = 3)             if (Double.parseDouble(textAnswer.getText() = 0)           

78、;    textAnswer.setText("除数不能为零");              clickable = false;                        else  

79、;             answerd /= vard;              textAnswer.setText(df.format(answerd);                &#

80、160;                     else           vard = Double.parseDouble(textAnswer.getText();          if (key = 0)  

81、0;          prekey = -1;            answerd += Double.parseDouble(textAnswer.getText();            textAnswer.setText(df.format(answerd); &#

82、160;                  if (key = 1)             prekey = -1;            answerd -= Double.parseDouble(textAnswe

83、r.getText();            textAnswer.setText(df.format(answerd);                    if (key = 2)           

84、  prekey = -1;            answerd *= Double.parseDouble(textAnswer.getText();            textAnswer.setText(df.format(answerd);          

85、60;         if (key = 3)             prekey = -1;            if (Double.parseDouble(textAnswer.getText() = 0)       

86、0;       textAnswer.setText("除数不能为零");              clickable = false;                     

87、0;  else               answerd /= Double.parseDouble(textAnswer.getText();              textAnswer.setText(df.format(answerd);      

88、60;                               prekey = 5;            /'%'操作,对第二个操作数除以100     

89、if (temp = buttonMod && clickable = true)         if (answerd = 0)           String s = textAnswer.getText();          textAnswer.setText(s);    &#

90、160;           else           boolean isDot = false;          for (int i = 0; i < textAnswer.getText().length(); i+)       

91、0;    if ('.' = textAnswer.getText().charAt(i)               isDot = true;              break;         

92、;             /如果是double数,除100          if (isDot = true)             double dtemp = Double.parseDouble(textAnswer.getText();   &#

93、160;        dtemp = dtemp / 100.0;            textAnswer.setText(Double.toString(dtemp);                    else  &

94、#160;          /如果是int数但能被100整除,则去掉末尾两个零            if (Integer.parseInt(textAnswer.getText() % 100 = 0)               int itemp = Inte

95、ger.parseInt(textAnswer.getText();              itemp /= 100;              textAnswer.setText(Integer.toString(itemp);        

96、0;               /如果是int数,但不能被100整除,则按double数处理            else               double dtemp = Double.parseDouble(tex

97、tAnswer.getText();              dtemp = dtemp / 100.0;              textAnswer.setText(Double.toString(dtemp);          

98、                                /开根号运算      if (temp = buttonSqrt && clickable = true)         St

99、ring s = textAnswer.getText();        if (s.charAt(0) = '-')           textAnswer.setText("负数不能开根号");          clickable = false;    

100、0;           else          textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double.              parseDouble(textAnswer.getText(); 

101、60;          /倒数运算      if (temp = buttonDao && clickable = true)         if (textAnswer.getText().charAt(0) = '0' &&          

102、;  textAnswer.getText().length() = 1)           textAnswer.setText("零不能求倒数");          clickable = false;                else

103、           boolean isDec = true;          int i, j, k;          String s = Double.toString(1 / Double.parseDouble(textAnswer.getText();     

104、     for (i = 0; i < s.length(); i+)            if (s.charAt(i) = '.')              break;          for

105、(j = i + 1; j < s.length(); j+)            if (s.charAt(j) != '0')               isDec = false;            &#

106、160; break;                      if (isDec = true)             String stemp = ""         &

107、#160;  for (k = 0; k < i; k+)              stemp += s.charAt(k);            textAnswer.setText(stemp);            

108、60;       else            textAnswer.setText(s);                    /按下'+/-'按钮时处理      if (temp =

109、 buttonAddAndSub && clickable = true)         boolean isNumber = true;        String s = textAnswer.getText();        for (int i = 0; i < s.length(); i+)     &

110、#160;    if (! (s.charAt(i) >= '0' && s.charAt(i) <= '9' | s.charAt(i) = '.' |                 s.charAt(i) = '-')        

111、60;    isNumber = false;            break;                  if (isNumber = true)           /如果当前字符串首字母有&#

112、39;-'号,代表现在是个负数,再按下时,则将首符号去掉          if (s.charAt(0) = '-')             textAnswer.setText("");            for (int i = 1;

113、 i < s.length(); i+)               char a = s.charAt(i);              textAnswer.setText(textAnswer.getText() + a);        

114、0;                       /如果当前字符串第一个字符不是符号,则添加一个符号在首字母处          else            textAnswer.setText(

115、'-' + s);                    /计算器有关内存操作      /'MC'的操作,将内存清0      if (temp = buttonMC && clickable = true)       

116、;  memoryd = memoryi = 0;        textMemory.setText("");            /'MS'的操作,将当前文本框内容保存入内存,显示'M'      if (temp = buttonMS && clickable = true)  &#

117、160;      boolean isDot = false;        textMemory.setText("   M");        for (int i = 0; i < textAnswer.getText().length(); i+)         

118、; if ('.' = textAnswer.getText().charAt(i)             isDot = true;            break;                 

119、; /如果是double,则存入memoryd(double存储器)        if (isDot = true)           memoryd = Double.parseDouble(textAnswer.getText();          memoryi = 0; /保证存储器中存放最新的值   &

120、#160;            /如果是int,则存入memoryi(int存储器)        else           memoryi = Integer.parseInt(textAnswer.getText();          memoryd = 0; /保证存储器中存放最新的值           

温馨提示

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

评论

0/150

提交评论