用Java设计计算器calculator内容提要:在本文构造实现了一个计算器擦亮calculator,主要内容包括:calculator的功能需求分析;calculator的基本设计思路和类的划分;calculator的具体实现关键字:Java、计算器calculator引言:设计实现一个Java应用程序的过程如下:(1) 功能需求分析;(2) 设计和类划分;(3) 代码编写实现本文就按照这个步骤来实现计算器calculator的制作正文:1. calculator功能需求分析作为计算器,至少应该具备以下几点功能:(1) 计算器要有GUI界面2) 能运算小数,并且不会产生精度损失3) 用户可以输入所需计算的数值,可以进行加、减、乘、除四种最基本的运算和混合运算4) 允许正负数间的运算5) 可以求一个数值的平方及倒数,可以进行阶乘运算6) 有菜单栏选项具体的功能需求和界面我们可以模仿微软公司出品的windowsXP中自带的计算器如图一: 图一 windows XP 中的计算器界面图2. calculator基本设计思路和类划分基于第1节中提出对于calculator功能需求的分析,对这个应用程序设计划分类如下:(1) calculator:这个类的功能是实现一个框架(2) calculatorFrame:这个类作为主类,实现主要功能:运算,按钮排布,菜单,颜色设置,文本显示3. calculator的具体实现3.1 calculator类的设计calculator用来定义一个计算器的框架1.主要方法下面以表格的形式列出calculator类至少应该具有的方法和功能描述(如表一所示)。
表一 calculator类的主要方法方法 功能描述static void main (String args[])calculator应用程序的入口,是主方法3.2 calculatorFrame类的设计 calculatorFrame类实现整体功能,包括窗体的初始化、各种用户事件监听和响应(菜单、运算、结果显示等等)1. 父类和主要接口设计calculator整体 窗口特性继承自JFrame类为了对用户命令做出响应(如帮助菜单栏弹出窗口),calculatorFrame类必须能够监听到用户的命令,因此设计calculatorFrame类实现ActionListener接口2. 主要方法下面以表格的形式列出calculatorFrame类至少应该具有的方法和功能描述(如表二所示)表二 calculatorFrame类的主要方法方法功能描述void actionPerformed(ActionEvent e)重载ActionListener接口中的方法,用于对用户命令进行响应,用户命令包括“帮助”“版权”“说明”以及各个按钮等3. 基本效果图二为calculator的基本效果图4. 代码分析calculator.java代码如下://calculator.java/**文件名:calculator.java*说明:calculatorFrame主类,实现主要功能*///导入AWT包import java.awt.*;import java.awt.event.*;//导入SWING包import javax.swing.*;import javax.swing.event.*; class calculator{ public static void main(String[] args) { calculatorFrame frame = new calculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); }}//主类calculatorFrameclass calculatorFrame extends JFrame implements ActionListener { public calculatorFrame() { //设置框架主题及大小setTitle("计算器"); setSize(300,235); //将面板置于框架中 Container contentPane = getContentPane(); setResizable(false); //创建按钮面板 JPanel buttonPanel = new JPanel(); //创建数字键“1” b11=new JButton ("1"); //设置颜色 b11.setForeground(Color. BLUE); //创建事件监听器 b11.addActionListener(this); b12=new JButton ("2"); b12.setForeground(Color. BLUE); b12.addActionListener(this); b13=new JButton ("3"); b13.setForeground(Color. BLUE); b13.addActionListener(this); b6=new JButton ("4"); b6.setForeground(Color. BLUE); b6.addActionListener(this); b7=new JButton ("5"); b7.setForeground(Color. BLUE); b7.addActionListener(this); b8=new JButton ("6"); b8.setForeground(Color. BLUE); b8.addActionListener(this); b1=new JButton ("7"); b1.setForeground(Color. BLUE); b1.addActionListener(this); b2=new JButton ("8"); b2.setForeground(Color. BLUE); b2.addActionListener(this); b3=new JButton ("9"); b3.setForeground(Color. BLUE); b3.addActionListener(this); b16=new JButton ("0"); b16.setForeground(Color. BLUE); b16.addActionListener(this); b17=new JButton ("+/-"); b17.setForeground(Color. BLUE); b17.addActionListener(this); b4=new JButton ("+"); b4.addActionListener(this); b9=new JButton ("-"); b9.addActionListener(this); b14=new JButton ("*"); b14.addActionListener(this); b19=new JButton ("/"); b19.addActionListener(this); b5=new JButton ("1/x"); b5.setForeground(Color. YELLOW); b5.addActionListener(this); b20=new JButton ("="); b20.setForeground(Color. YELLOW); b20.addActionListener(this); //”.”显示不清晰,故采用“。
代替b18=new JButton ("");b18.setForeground(Color. BLUE); b18.addActionListener(this); b10=new JButton ("x^2"); b10.setForeground(Color. YELLOW); b10.addActionListener(this); b15=new JButton ("n!"); b15.setForeground(Color. YELLOW); b15.addActionListener(this); buttonPanel.add(b1); buttonPanel.add(b2); buttonPanel.add(b3); buttonPanel.add(b4); buttonPanel.add(b5); buttonPanel.add(b6); buttonPanel.add(b7); buttonPanel.add(b8); buttonPanel.add(b9); buttonPanel.add(b10); buttonPanel.add(b11); buttonPanel.add(b12); buttonPanel.add(b13); buttonPanel.add(b14); buttonPanel.add(b15); buttonPanel.add(b16); buttonPanel.add(b17); buttonPanel.add(b18); buttonPanel.add(b19); buttonPanel.add(b20); //对按钮面板1进行排版,四行五列 buttonPanel.setLayout(new GridLayout(4,5)); //建立容纳文本框的面板 JPanel textPanel = new JPanel(); addText = new JTextField("0" , 20); addText.addActionListener(this); //文本框从右端开始显示 addText.setHorizontalAlignment(JTextField.RIGHT); textPanel.add(addText); //创建按钮面板2 JPanel buttonPanel2=new JPanel(); b21=new JButton ("DEL "); b21.addActionListener(this); b21.setForeground(Color. RED); b22=new JButton ("CE"); b22.addActionListener(this); b22.setForeground(Color. RED); b23=new JButton ("C"); b23.addActionListener(this); b23.setForeground(Color. RED); buttonPanel2.add(b21); buttonPanel2.add(b22); buttonPanel2.add(b23); buttonPanel2.setLayout(new GridLayout(1,5)); //创建菜单栏,并将菜单栏加入到框架中 JMenuBar menuBar=new JMenuBar(); setJMenuBar(menuBar); //创建“帮助(H) ”菜单 JMenu helpMenu=new JMenu("帮助(H)"); helpMenu.setForeground(Color. BLUE); //设置菜单的快捷键 helpMenu.setMnemonic('H'); menuBar.add(helpMenu); JMenuItem copyrightItem = new JMenuItem("版权", 'b'); copyrightItem.addActionListener(this); JMenuItem explainItem=new JMenuItem("说明",'a'); explainItem.addActionListener(this); //将菜单项添加到“帮助”菜单中 helpMenu.add(copyrightItem); helpMenu.add(explainItem); //设置面板布局 contentPane.add(buttonPanel, BorderLayout.SOUTH); contentPane.add(buttonPanel2, BorderLayout.CENTER); contentPane.add(textPanel, BorderLayout.NORTH); } //设置用户点击菜单项和按钮时的响应动作 public void actionPerformed(ActionEvent e) { if (e.getActionCommand()=="版权") { int selection=JOptionPane.showConfirmDialog( calculatorFrame.this, "制作人:5409 金华日,5403 章旭,5397 李伏俊","版权", JOptionPane.DEFAULT_OPTION); } if(e.getActionCommand()=="说明") { int selection=JOptionPane.showConfirmDialog( calculatorFrame.this, "此计算器可进行多种常规运算,欢迎大家使用。
","帮助", JOptionPane.DEFAULT_OPTION); } if((addText.getText()).equals("0")) { addText.setText(" "); } if(e.getSource()==b16) {c+="0"; addText.setText(""+c);} if(e.getSource()==b11) {c+="1"; addText.setText(""+c);} if(e.getSource()==b12) {c+="2"; addText.setText(""+c);} if(e.getSource()==b13) {c+="3"; addText.setText(""+c);} if(e.getSource()==b6) {c+="4"; addText.setText(""+c);} if(e.getSource()==b7) {c+="5"; addText.setText(""+c);} if(e.getSource()==b8) {c+="6"; addText.setText(""+c);} if(e.getSource()==b1) {c+="7"; addText.setText(""+c);} if(e.getSource()==b2) { c+="8"; addText.setText(""+c);} if(e.getSource()==b3) { c+="9"; addText.setText(""+c);} if(e.getSource()==b18) { boolean clickable=true; for (int i = 0; i < addText.getText().length(); i++) if ('.' == addText.getText().charAt(i)) { clickable=false; break; } if(clickable) {c+="."; } addText.setText(c); } //平方 if(e.getSource()==b10) { nu =Double.parseDouble(addText.getText()); num=nu*nu; addText.setText(String.valueOf(num)); c=""; } //倒数 if(e.getSource()==b5) { nu =Double.parseDouble(addText.getText()); if(addText.getText().charAt(0)=='0'&&addText.getText().length()==1) addText.setText("除数不能为0"); else num=1/nu; addText.setText(String.valueOf(num)); c=""; } //阶乘 if(e.getSource()==b15) { c="";num=1; nu =Double.parseDouble(addText.getText()); for (int n=1;n<=nu;n++) { num=num*n; addText.setText(String.valueOf(num)); } }//响应“+/-”按钮 if(e.getSource()==b17) { String s=addText.getText(); if(addText.getText().charAt(0)=='-') { addText .setText(""); for (int i=1;i
两个类中的两个类似的功能,调用他们的类动态的决定一种实现,那他们提供一个抽象父类,子类分别实现父类所定义的方法问题的出现:Java是一种单继承的语言,一般情况下,哪个具体类可能已经有了一个超类,解决是给它的父类加父类,或者给它父类的父类加父类,直到移动到类等级结构的最顶端这样一来,对一个具体类的可插入性的设计,就变成了对整个等级结构中所有类的修改在本次实验计算器中每个按钮功能的实现都离不开监听器,监听器的接口以及实现将是java程序的主要内容计算器菜单栏的实现就是监听器的具体表现,当监听器发现“版权”指令时(e.getActionCommand()=="版权"),监听器就会做出反应,弹出一个对话框,显示“制作人:5409 金华日,5403 章旭,5397 李伏俊","版权"”,如果监听器接到指令(e.getSource()==b16)时,就会执行下面的功能(c+="0";addText.setText(""+c);),就是在文本框中输出数字“0”if(e.getActionCommand()=="CE"){addText.setText("0"); c="";}当监听器接到指令“CE”时,就会自动清除文本框中的内容,初始文本框为“0”。
当监听器收到指令为“DEL”时(if(e.getSource()==b21){String g=addText.getText();addText.setText("");for(inti=0;i
二、实验要求:(1)问题描述准确、规范;(2)程序结构合理,调试数据准确、有代表性;(3)界面布局整齐,人机交互方便;(4)输出结果正确;(5)正确撰写实验报告三、实验内容:(1) 用Java编写文本编辑器,综合掌握Java编程技巧及Java开发环境;(2) 要求该记事本可以设置文本的字体、大小、颜色等基本参数;(3) 可以读取计算机中TXT文件,可以生成一个新的TXT文件四、实验步骤:(1) 上机实验之前,学生应当为课程设计的内容作好充分准备对每次上机需要完成的任务进行认真的分析,画出程序流程图,手工写出符合任务要求的程序清单,准备出调试程序使用的数据,以便提高上机实验的效率2) 按照实验目的和实验内容进行上机操作录入程序,编译调试,反复修改,直到使程序正常运行,得出正确的输出结果为止3) 根据实验结果,写出实验报告实验报告应当包括:实验内容,程序流程图,类结构,程序清单,运行结果,以及通过上机取得的经验五、实验结果:图2-1文本编辑器的界面效果图图2-2字体大小及格式处理后的效果图六、实验体会: Java编程文本编辑器就是微软记事本的“缩略版”,为什么这么说呢,文本编辑器的功能之一就是实现文本的录入,像记事本一样写入字体,以及字体的颜色、大小和字型的改变,最主要的功能就是实现TXT文件的生成,即生成文件的格式是TXT格式。
当然不单单只有这些功能,本编辑器还能实现文件的打开和保存等功能在本次实验中按钮功能的实现必须依靠监听器的功能,菜单栏中“文件”的功能就包括本地文件的打开和保存,当监听器接受到“文件——打开”这个指令时,就会弹出一个如图2—4的对话框,列出本地的文件菜单当监听器接到“格式——颜色——RED”这个指令时,就会改变文本编辑区的文字颜色,由原来的黑色变为红色但是这是有缺点的,文字颜色的改变是所有文字颜色的改变,而不能改变其中的某些文字,所有的字体、字型的改变都是整体性的改变,不能局部发生变化,这也是我们在实验中唯一没有做好的一部分,也是所有同学都没法解决的问题,就是微软的记事本也没有解决这个问题我想在以后的学习中,随着能力的改变,一定会解决类似的问题,争取把实验做得更好七、附件:(1) 程序流程图:引入各种图形所需要的包;编写公共类edit,定义各种变量; 编写textFrame类;添加菜单栏,并向各菜单添加菜单项及子菜单;添加带滚动条的文本域及工具栏;为各菜单及工具添加事件监听器;编写actionPerformed方法;实现事件监听器,响应各菜单项(2) 程序清单://editor.java//引入图形界面所需要的包import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;//创建窗口public class editor{ public static void main (String[] args) { textFrame frame=new textFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); }}class textFrame extends JFrame implements ActionListener{ public textFrame() { //设置窗口标题及大小 setTitle("记事本"); setSize(600,540); //添加菜单 JMenuBar menubar =new JMenuBar(); setJMenuBar(menubar); //新建文件菜单 fileMenu =new JMenu("文件(F)"); fileMenu.setForeground(Color. BLUE);//设置菜单颜色 fileMenu.setMnemonic('F');//设置快捷键 //创建文件下的各菜单项并添加事件监听器 openitem=new JMenuItem("打开"); openitem.addActionListener(this); saveitem=new JMenuItem("保存"); saveitem.addActionListener(this); //添加菜单项 fileMenu.add(openitem); fileMenu.add(saveitem); //新建格式菜单 JMenu styleMenu =new JMenu("格式(S)"); styleMenu.setForeground(Color. BLUE); styleMenu.setMnemonic('S'); //新建格式菜单下的子菜单 JMenu colorMenu=new JMenu("颜色"); JMenu fontMenu=new JMenu("字体"); styleMenu.add(colorMenu); //创建颜色菜单项并添加事件监听器 bitem=new JMenuItem("BLUE"); bitem.addActionListener(this); bitem.setForeground(Color. BLUE); blitem=new JMenuItem("BLACK"); blitem.addActionListener(this); blitem.setForeground(Color. BLACK); ritem=new JMenuItem("RED");ritem.addActionListener(this); ritem.setForeground(Color. RED); gitem=new JMenuItem("GREEN"); gitem.addActionListener(this); gitem.setForeground(Color. GREEN); //添加菜单项 colorMenu.add(bitem); colorMenu.add(blitem);colorMenu.add(ritem); colorMenu.add(gitem); styleMenu.add(new JMenuItem("_____")); styleMenu.add(fontMenu); //创建并添加形体菜单项并添加事件监听器 songitem=new JMenuItem("宋体"); songitem.addActionListener(this); huaitem=new JMenuItem("华文行楷"); huaitem.addActionListener(this); liitem=new JMenuItem("隶书"); liitem.addActionListener(this); fontMenu.add(songitem); fontMenu.add(huaitem); fontMenu.add(liitem); fontMenu.add(new JMenuItem("_____")); //创建复选菜单并添加事件监听器 bolditem=new JCheckBoxMenuItem("加粗"); bolditem.addActionListener(this); italicitem=new JCheckBoxMenuItem("倾斜"); italicitem.addActionListener(this); fontMenu.add(bolditem); fontMenu.add(italicitem); //新建帮助菜单并设置颜色及快捷键 JMenu helpMenu=new JMenu("帮助(H)"); helpMenu.setForeground(Color. BLUE);helpMenu.setMnemonic('H');//创建帮助菜单项并添加事件监听器copyrightitem = new JMenuItem("版权", 'b');copyrightitem.addActionListener(this);explainitem=new JMenuItem("说明",'a');explainitem.addActionListener(this);helpMenu.add(copyrightitem);helpMenu.add(explainitem);//添加各菜单menubar.add(fileMenu);menubar.add(styleMenu);menubar.add(helpMenu);Container contentPane=getContentPane();//新建文本面板JPanel TextPanel=new JPanel();//设置文本域大小text=new JTextArea(27,80);//设置初始字体text.setFont(new Font("宋体",Font.PLAIN,13));//为文本域添加滚动条JScrollPane scrollpane=new JScrollPane (text);TextPanel.add(scrollpane);contentPane.add(TextPanel,BorderLayout.SOUTH);//新建工具面板JPanel toolPanel=new JPanel();//创建标签JLabel l=new JLabel("字体大小");//新建组合框sizeBox= new JComboBox();//创建用来存放字体大小的数组并进行初始化a=new int [16];for(int i=0;i<=15;i++){ a[i]=(i+5)*2;sizeBox.addItem(""+a[i]);}//为组合框添加并实现事件监听器 sizeBox.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent event) { if( event.getStateChange() == ItemEvent.SELECTED) { text.setFont( new Font( text.getFont().getName(), text.getFont().getStyle(), a[sizeBox.getSelectedIndex()] ) ); } } } ); toolPanel.add(l); toolPanel.add(sizeBox); contentPane.add(toolPanel,BorderLayout.NORTH); }//实现事件监听器public void actionPerformed(ActionEvent e){Object s=e.getSource();//实现版权菜单项if (s==copyrightitem){int selection=JOptionPane.showConfirmDialog(textFrame.this,"制作人:5409 金华日。