中用GUI编程实现计算器模拟_第1页
中用GUI编程实现计算器模拟_第2页
中用GUI编程实现计算器模拟_第3页
中用GUI编程实现计算器模拟_第4页
中用GUI编程实现计算器模拟_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

1、JAVA中用GUI编程实现计算器模拟摘 要:本文论述了用java对计算器程序进行需求分析、概要设计、详细设计,最后使用Java编程实现的全过程。设计GUI界面的计算器,用户可以通过鼠标依次输入参加计算的数值,进行加、减、乘、除及求负、取余等,主要用到的组件有框架、面板、文本框、按钮和菜单栏等。关键词:GUI 计算器 一.编程思想定义计算器类calculator,该类继承JFrame类并实现ActionListener接口。整体窗口采用边界布局,通过另外建立若干面板组件。North位置的面板上放置一个文本框,用于显示运算中的数据及结果;center位置放置两个按钮,分别用来返回原界面和清零。再用

2、GridLayout(网格布局)对数字控件和操作按钮进行布局。此外,单击任何一个按钮都会触发ActionEvent事件,要处理这些事件就必须实现ActionListener接口的actionPerformed方法。二程序代码如下import javax.swing.*;import javax.swing.event.*;import java.awt.*;.*;/变量设置如下/继承JFrame类并实现ActionListenter接口public class calculator extends JFrame implements ActionListenerJFrame frame;/数字

3、及操作按钮JButton num0,num1,num2,num3,num4,num5,num6,num7,num8,num9,Back,c;/运算按钮JButton plusButton,minusButton,multiplyButton,divideButton,residueButton,equalButton,changeButton,dotButton,sqrtButton,reciprocalButton;JMenu fileM;JMenuItem exitM,helpM;JTextField text;/状态变量boolean clicked=true;boolean clear

4、=true;int all=0;double previous;String fuhao;int first=1;/界面设计public calculator()setTitle("计算器");setSize(300,250);setLocation(400,400);text=new JTextField(25);text.setHorizontalAlignment(JTextField.LEFT);JPanel cp1=new JPanel();JPanel cp2=new JPanel();JPanel cp3=new JPanel();/设置整体布局getCont

5、entPane().add(cp1,"North");getContentPane().add(cp2,"Center");getContentPane().add(cp3,"South");cp1.setLayout(new GridLayout(1,1);cp2.setLayout(new GridLayout(1,2);cp3.setLayout(new GridLayout(5,4);plusButton=new JButton("+");/加minusButton=new JButton("-&

6、quot;);/减multiplyButton=new JButton("*");/乘divideButton=new JButton("/");/除residueButton=new JButton("%");/余数equalButton=new JButton("=");/等号changeButton=new JButton("+/-");/正负号切换dotButton=new JButton(".");/小数点sqrtButton=new JButton("s

7、qrt");/开方reciprocalButton=new JButton("1/x");/倒数/数字按钮num0=new JButton("0");num1=new JButton("1");num2=new JButton("2");num3=new JButton("3");num4=new JButton("4");num5=new JButton("5");num6=new JButton("6");num7=new

8、 JButton("7");num8=new JButton("8");num9=new JButton("9");cp1.add(text);text.setEditable(false);text.setBackground(Color.white);/监听设置Back=new JButton("Back");Back.addActionListener(this);c=new JButton("C");c.addActionListener(this); cp2.add(Back);cp2

9、.add(c);cp3.add(num7);num7.addActionListener(this);cp3.add(num8);num8.addActionListener(this);cp3.add(num9);num9.addActionListener(this);cp3.add(divideButton);divideButton.addActionListener(this);cp3.add(num4);num4.addActionListener(this);cp3.add(num5);num5.addActionListener(this);cp3.add(num6);num6

10、.addActionListener(this);cp3.add(multiplyButton);multiplyButton.addActionListener(this);cp3.add(num1);num1.addActionListener(this); cp3.add(num2);num2.addActionListener(this);cp3.add(num3);num3.addActionListener(this);cp3.add(minusButton);minusButton.addActionListener(this); cp3.add(num0);num0.addAc

11、tionListener(this);cp3.add(changeButton);changeButton.addActionListener(this);cp3.add(dotButton);dotButton.addActionListener(this);cp3.add(plusButton);plusButton.addActionListener(this);cp3.add(reciprocalButton);reciprocalButton.addActionListener(this);cp3.add(residueButton);residueButton.addActionL

12、istener(this);cp3.add(sqrtButton);sqrtButton.addActionListener(this);cp3.add(equalButton);equalButton.addActionListener(this);/添加菜单组件JMenuBar mainMenu= new JMenuBar();setJMenuBar(mainMenu);JMenu editMenu=new JMenu("文件");JMenu helpMenu=new JMenu("帮助");helpM= new JMenuItem("关于

13、");mainMenu.add(editMenu);mainMenu.add(helpMenu);helpMenu.add(helpM);helpM.addActionListener(this);fileM=new JMenu(" 文件");exitM=new JMenuItem(" 退出");fileM.addActionListener(this);exitM.addActionListener(this);editMenu.add(fileM); editMenu.add(exitM);setVisible(true);addWindo

14、wListener(new WindowDestroyer();/结束窗口 /响应动作代码 public void actionPerformed(ActionEvent e)if (first =1)text.setText("");first=0;Object temp=e.getSource();if (temp=exitM)System.exit(0);if (temp=helpM)JOptionPane.showMessageDialog(null,"请查看帮助文档","帮助",JOptionPane.INFORMATION

15、_MESSAGE);/退格if (temp= Back)String s=text.getText();text.setText("");for (int i=0;i<s.length()-1;i+)char a=s.charAt(i);text.setText(text.getText()+a);/清零if (temp=c)text.setText("0.");clear=true;first=1;if (temp=num0)/判断是否单击了符号位if (clear=false)text.setText("");text.se

16、tText(text.getText()+"0"); if (temp=num1)if (clear=false)text.setText("");text.setText(text.getText()+"1");clear=true;if (temp=num2)if (clear=false)text.setText("");text.setText(text.getText()+"2"); clear=true; if (temp=num3)if (clear=false)text.setT

17、ext("");text.setText(text.getText()+"3"); clear=true; if (temp=num4)if (clear=false)text.setText("");text.setText(text.getText()+"4");clear=true; if (temp=num5)if (clear=false)text.setText("");text.setText(text.getText()+"5"); clear=true; i

18、f (temp=num6)if (clear=false)text.setText("");text.setText(text.getText()+"6");clear=true; if (temp=num7)if (clear=false)text.setText("");text.setText(text.getText()+"7");clear=true; if (temp=num8)if (clear=false)text.setText("");text.setText(text.ge

19、tText()+"8"); clear=true; if (temp=num9)if (clear=false)text.setText("");text.setText(text.getText()+"9"); clear=true;/判断是否含有小数点 if (temp = dotButton) clicked=true; for (int i=0;i<text.getText().length();i+) if ('.'=text.getText().charAt(i) clicked = false; b

20、reak; if (clicked = true) text.setText(text.getText()+".");try if (temp=plusButton) previous=Double.parseDouble(text.getText(); fuhao="+" clear=false; if (temp=minusButton) previous=Double.parseDouble(text.getText(); fuhao="-" clear=false; if (temp=multiplyButton) previ

21、ous=Double.parseDouble(text.getText(); fuhao="*" clear=false; if (temp=divideButton) previous=Double.parseDouble(text.getText(); fuhao="/" clear=false; if (temp=equalButton) double next = Double.parseDouble(text.getText(); text.setText(""); if (fuhao ="+") tex

22、t.setText(previous + next +""); if (fuhao ="-") text.setText(previous - next +""); if (fuhao ="*") text.setText(previous * next +""); if (fuhao ="/") if (next=0) text.setText("除数不能为零"); else text.setText(previous + next +"&qu

23、ot;); if (fuhao ="%") text.setText(previous % next +""); clear=false; if (temp=sqrtButton) String s=text.getText();if (s.charAt(0)= '-')text.setText("负数不能开根号");elsetext.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText();clear = false;

24、if (temp=reciprocalButton)if (text.getText().charAt(0)='0'&& text.getText().length()=1)text.setText("除数不能为零");elseboolean isDec = true;int i,j,k;String s=Double.toString(1/Double.parseDouble(text.getText();for (i=0;i<s.length();i+)if (s.charAt(i)='.')break;for (j

25、=i+1;j<s.length();j+)if (s.charAt(j)='0')isDec=false;break;if (isDec = true) String stemp = ""for (k=0;k<i;k+)stemp += s.charAt(k);text.setText(stemp); elsetext.setText(s); clear = false; if ( temp = residueButton) previous=Double.parseDouble(text.getText(); fuhao="%" clear=false; if ( temp = changeButton) boolean isNumber = true; String s = text.getText(); for (int i=0;i<s.length();i+) if (!(s.charAt(i)>= '0'&& s.charAt(i

温馨提示

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

最新文档

评论

0/150

提交评论