.word范文《Java程序设计》实验指导书计算机科学与软件学院[软件151][张路][153200].word范文.word范文实验四图形用户界面程序设计实验目的:掌握组件的使用方法,理解委托事件处理模型。熟悉图形用户界面基本组件的使用方法,熟悉如何使用布局管理器对组件进行管理及如何使用Java的事件处理机制。实验内容:1、输入一个整数,分别显示其百位、十位和个位数字,图形用户界面如图4.1所示。要求:整数文本行可编辑且能实现事件处理,当输入数据错误时,处理异常,弹出对话框,提示重新输入信息;其他文本行仅用于显示不可编辑。1.importjava.awt.*;2.importjava.awt.event.*;3.importjavax.swing.*;4.5.6.publicclassex_1extendsJFrameimplementsActionListener7.{8./**9.*10.*/11.privatestaticfinallongserialVersionUID=1L;12.privateMessageDialogdialog;//对话框内部类对象13.TextFieldtext_num,text_hun,text_dec,text_uni;14.15.publicex_1()16.{17.super(显示整数数字);18.this.setSize(300,240);19.this.setLocation(300,240);20.this.setBackground(Color.lightGray);//背景颜色:亮灰21.//窗口关闭按钮,结束程序运行22.this.setDefaultCloseOperation(3);23.this.setLayout(newGridLayout(4,2));//流体布局:4行2列24.25.this.add(newLabel(整数,0));26.text_num=newTextField();27.this.add(text_num);28.text_num.addActionListener(this);//注册文.word范文本编辑事件监视器29.30.this.add(newLabel(百位,0));31.text_hun=newTextField();32.text_hun.setEditable(false);//只显示,不允许编辑33.this.add(text_hun);34.35.this.add(newLabel(十位,0));36.text_dec=newTextField();37.text_dec.setEditable(false);38.this.add(text_dec);39.40.this.add(newLabel(个位,0));41.text_uni=newTextField();42.text_uni.setEditable(false);43.this.add(text_uni);44.45.this.setVisible(true);46.dialog=newMessageDialog(this);47.}48.49.50.privateclassMessageDialogextendsJDialog51.{52./**53.*54.*/55.privatestaticfinallongserialVersionUID=1L;56.Frameframe;//对话框所依赖的框架窗口57.Labellabel;//对话框中显示信息58.MessageDialog(Frameframe)59.{60.super(frame,消息,true);61.this.frame=frame;62.this.setSize(300,80);63.label=newLabel(,Label.CENTER);64.this.add(label);65.this.setDefaultCloseOperation(HIDE_ON_CLOSE);66.}67.publicvoidshow(Stringstring)68.{.word范文69.label.setText(string);70.this.setLocation(frame.getX()+100,frame.getY()+100);71.this.setVisible(true);72.}73.74.}75.76.publicvoidactionPerformed(ActionEvente)77.{78.try79.{80.intx=(int)Double.parseDouble(text_num.getText());81.text_uni.setText(uni(x));82.text_dec.setText(dec(x));83.text_hun.setText(hun(x));84.}85.catch(NumberFormatExceptionnfe)86.{87.dialog.show(\+text_num.getText()+\不能转换为整数,请重新输入);88.}89.finally{}90.}91.92.publicstaticStringuni(intx)93.{94.Stringresult=+x%10;95.returnresult;96.}97.publicstaticStringhun(intx)98.{99.inty=(x%1000-x%100)/100;100.Stringresult=+y;101.returnresult;102.}103.publicstaticStringdec(intx)104.{105.inty=(x%100-x%10)/10;106.Stringresult=+y;107.returnresult;108.}109.110.publicstaticvoidmain(Stringarg[])111.{.word范文112.newex_1();113.}114.}2、模拟实现一个可视化的简单计算器,至少提供进行加法、减法、乘法、除法等基本运算的功能,希望能支持加正负号、求平方根、清零等其他功能。1.importjava.awt.*;2.importjava.awt.event.*;3.importjavax.swing.*;4.5.publicclasscalculatorextendsJFrameimplementsActionListener{6.7.privatestaticfinallongserialVersionUID=-169068472193786457L;8.9.privateclassWindowCloserextendsWindowAdapter{10.publicvoidwindowClosing(WindowEventwe){11.System.exit(0);12.}13.}14.15.16.privatefinalString[]str={sqrt,+/-,CE,C,7,8,9,/,4,5,6,*,1,2,3,-,.,0,=,+};17.JButton[]buttons=newJButton[str.length];18.JTextFielddisplay=newJTextField(0);19.doublenumber=0.0;.word范文20.Stringoperator==;21.booleanisFirstDigit=true;22.23.publiccalculator(){24.super(Calculator);25.this.setSize(800,600);26.this.setLocation(300,240);27.28.JPanelpanel1=newJPanel(newGridLayout(5,4));//面板1网格布局29.for(inti=0;istr.length;i++){//加入数字符号键30.buttons[i]=newJButton(str[i]);31.panel1.add(buttons[i]);32.}33.34.JPanelpanel2=newJPanel(newBorderLayout());//面板2边界布局35.panel2.add(Center,display);//加入显示框36.37.getContentPane().setLayout(newBorderLayout());38.getContentPane().add(North,panel2);39.getContentPane().add(Center,panel1);40.for(inti=0;istr.length;i++)41.buttons[i].addActionListener(this);42.display.addActionListener(this);43.addWindowListener(newWindowCloser());44.setVisible(true);45.pack();46.}47.publicvoidactionPerformed(ActionEvente){48.Stringlabel=e.getActionCommand();//调取按钮上字符串49.if(label==CE||label==C)50.handleReset();//清零51.elseif(0123456789..indexOf(label)=0)52.handleNumber(label);//数字的输入53.elseif(label==sqrt||label==+/-)54.handleMonocular(label);//单目运算55.else56.handleOperator(label);//双目运算+-*/57.}58.59.privatevoidhandleMonocular(Stringkey){.word范文60.if(key.equals(+/-))61.number=-Double.valueOf(display.getText());62.elseif(key.equals(sqrt))63.number=Math.sqrt(Double.valueOf(display.getText()));64.display.setText(String.valueOf(number));65.}66.67.publicvoidhandleNumber(Stringkey){68.if(isFirstDigit)69.display.setText(key);70.elseif((key.equals(.))&&(display.getText().indexOf(.)0))71.display.setText(display.getText()+.);72.elseif(!key.equals(.))73.display.setText(display.getText()+key);74.isFirstDigit=false;75.}76.77.publicvoidhandleReset(){78.display.setText(0);79.isFirstDigit=true;80.operator==;81.}82.83.publicvoidhandleOperator(Stringkey){84.if(operator.equals(-))85.number-=Double.valueOf(display.getText());86.elseif(operator.equals(*))87.number*=Double.valueOf(display.getText());88.elseif(operator.equals(/))89.number/=Double.valueOf(display.getText());90.elsei