目录1概述····································································12系统需求分析···························································12.1系统目标·······································································12.2主体功能·······································································13系统概要设计···························································23.1系统的功能模块划分························································23.2系统流程图····································································24系统详细设计···························································24.1主界面模块详细设计·························································24.2事件响应模块详细设计······················································55测试····································································65.1测试方案·······································································65.2测试结果·······································································76小结····································································77参考文献·································································8源代码·······································································911概述近年来随着计算机和网络在社会领域的发展,java的应用正在不断地走向深入,Java语言的优良特性使得Java应用具有无比的健壮性和可靠性,这也减少了应用系统的维护费用。Java对对象技术的全面支持和Java平台内嵌的API能缩短应用系统的开发时间并降低成本。Java的编译一次,到处可运行的特性使得它能够提供一个随处可用的开放结构和在多平台之间传递信息的低成本方式。特别是Java企业应用编程接口为企业计算及电子商务应用系统提供了有关技术和丰富的类库。本次设计是通过java编程技术来设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算,该计算器可以实现加法、减法、乘法、除法的简单运算,也可以实现一些简单的扩展运算。2系统需求分析2.1系统目标一个简单的计算器程序。用户可以利用鼠标点击数值或操作符按键完成计算的输入,计算的结果将被显示在窗口上部的文本框中。2.2主体功能1.按下数字键在文本框上会显示数字,这是计算器最基本的功能。2.在任何时候按下“+-”键,计算器文本框上的数字置反。3.上面一个文本框显示计算过程,下面一个文本框显示输入的数字。4.当按下的运算符号时前面已经按下过运算符号时,下面一个文本框显示上一个运算符号以及两个数之间的运算结果。5.按下清除“C”键,数据被全部清除。23系统概要设计3.1系统的功能模块划分1.主界面模块2.事件相应模块3.2系统流程图4系统详细设计4.1主界面模块详细设计顶层容器:ComputerPad类实现主窗口,顶层容器中包含了1个panel容器,1个resultshow文本框,1个process文本框。各组件作用:其中panel容器用于放数字按钮,小数点,运算符按钮,清零按钮,正负转换按钮以及退格按钮。两个文本框一个用于显示输入数据和计算结果,另一3个用于显示计算过程。布局方式:顶层容器用BroadLayOut布局,process和resultshow文本框以及panel容器分别放在定岑容器的noth,center以及south面,panel容器用GridLayOut布局。部分代码如下:数字按钮类:实现数字按钮的定义publicclassNumberButtonextendsButton{}运算符按钮类:实现运算符号的定义publicclassOperationButtonextendsButton{}主窗口类实现监视器的注册,窗口布局,组件颜色大小等的设置publicclassComputerPadextendsFrameimplementsActionListener{NumberButtonnumberButton[];OperationButtonoprationButton[];Button小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮;Panelpanel;JTextFieldresultShow;JTextFieldprocess;String运算符号[]={+,-,*,/};LinkedList链表;boolean是否按下等号=false;publicComputerPad(){super(计算器);链表=newLinkedList();//进行组件的声明,初始化设置,注册监视器numberButton=newNumberButton[10];for(inti=0;i=9;i++){numberButton[i]=newNumberButton(i);numberButton[i].addActionListener(this);}oprationButton=newOperationButton[4];for(inti=0;i4;i++){oprationButton[i]=newOperationButton(运算符号[i]);oprationButton[i].addActionListener(this);}小数点按钮=newButton(.);正负号按钮=newButton(+/-);等号按钮=newButton(=);求倒数按钮=newButton(1/x);退格按钮=newButton(退格);清零按钮=newButton(C);清零按钮.setForeground(Color.red);退格按钮.setForeground(Color.red);4等号按钮.setForeground(Color.red);求倒数按钮.setForeground(Color.blue);正负号按钮.setForeground(Color.blue);小数点按钮.setForeground(Color.blue);退格按钮.addActionListener(this);清零按钮.addActionListener(this);等号按钮.addActionListener(this);小数点按钮.addActionListener(this);正负号按钮.addActionListener(this);求倒数按钮.addActionListener(this);//对显示过程的文本框进行设置process=newJTextField(10);process.setHorizontalAlignment(JTextField.LEFT);process.setForeground(Color.blue);process.setFont(newFont(TimesRoman,Font.PLAIN,14));process.setBorder(newSoftBevelBorder(BevelBorder.LOWERED));process.setBackground(Color.white);process.setEditable(false);//对显示输入值以及运算结果的文本框进行设置resultShow=newJTextField(10);resultShow.setHorizontalAlignment(JTextField.RIGHT);resultShow.setForeground(Color.blue);resultShow.setFont(newFont(TimesRoman,Font.PLAIN,14));resultShow.setBorder(newSoftBevelBorder(BevelBorder.LOWERED));resultShow.setBackground(Color.white);resultShow.setEditable(false);//进行主窗口布局panel=newPanel();panel.setLayout(newGridLayout(5,4));Panel.add(清零按钮);Panel.add(退格按钮);panel.add(numberButton[1]);panel.add(numberButton[2]);panel.add(numberButton[3]);Panel.add(numberButton[4]);panel.add(numberButton[5]);panel.add(numberButton[6]);Panel.add(numberButton[7]);panel.add(numberButton[8]);panel.add(numberButton[9]);Panel.add(numberButton[0]);Panel.add(oprationButton[0]);panel.add(oprationButton[1]);panel.add(oprationButton[2]);panel.add(oprationButton[3]);panel.add(求倒数按钮);panel.add(正负号按钮);panel.add(小数点按钮);5Panel.add(等号按钮);add(panel,BorderLayout.SOUTH);add(resultShow,BorderLayout.CENTER);add(process,BorderLayout.NORTH);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setVisible(true);setBounds(100,50,170,190);setResizable(false);validate();4.2事件响应模块详细设计模块功能:事件响应模块用于实现数据输入,保存,运算以及显示。具体实现:将主窗口作为窗口中所有组件的监视器,链表用来存储输入的数字以及操作符以便运算时提取出来,监视器用if,elseif语句分数字按钮,小数点,运算符按钮,清零按钮,正负转换按钮以及退格按钮6种情况进行相应处理,其中除了清零按钮每种情况又分成链表长度为1,2,3作相应的处理。事件响应模块的框架代码如下:publicvoidactionPerformed(ActionEvente){按下数字按钮时的事件处理if(e.getSource()instanceofNumberButton){