欢迎来到天天文库
浏览记录
ID:37852034
大小:544.00 KB
页数:8页
时间:2019-06-01
《JAVA常用GUI界面与事件处理》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、附完整源码:ComputerFrame.Javaimportjava.awt.*;importjava.awt.event.*;publicclassComputerFrameextendsFrameimplementsActionListener{TextFieldtextOne,textTwo,textResult;ButtongetProblem,giveAnswer;LabeloperatorLabel,message;Teacherteacher;ComputerFrame(Strings){super(s)
2、;teacher=newTeacher();setLayout(newFlowLayout());textOne=newTextField(10);//创建textOne,其可见字符长时10textTwo=newTextField(10);//创建textTwo,其可见字符长时10textResult=newTextField(10);//创建textResult,其可见字符长时10operatorLabel=newLabel("+");message=newLabel("你还没回答呢");getProblem=new
3、Button("获取题目");giveAnswer=newButton("确认答案");add(getProblem);add(textOne);add(operatorLabel);add(textTwo);add(newLabel("="));add(textResult);add(giveAnswer);add(message);textResult.requestFocus();textOne.setEditable(false);textTwo.setEditable(false);//将当前窗口注册为get
4、Problem的ActionEvent事件监视器getProblem.addActionListener(this);//将当前窗口注册为giveAnswer的ActionEvent事件监视器giveAnswer.addActionListener(this);//将当前窗口注册为textResult的ActionEvent事件监视器textResult.addActionListener(this);setBounds(100,100,450,100);setVisible(true);//设置可见性validate
5、();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(1);}});}publicvoidactionPerformed(ActionEvente){if((Button)e.getSource()==getProblem)//判断事件源是否是getProblem{intnumber1=teacher.giveNumberOne(100);intnumber2=teacher.giveNumbe
6、rTwo(100);Stringoperator=teacher.giveOperator();textOne.setText(""+number1);textTwo.setText(""+number2);operatorLabel.setText(operator);message.setText("请回答");textResult.setText(null);}if((Button)e.getSource()==giveAnswer)//判断事件源是否是giveAnswer{Stringanswer=textRe
7、sult.getText();try{intresult=Integer.parseInt(answer);if(teacher.getRight(result)==true){message.setText("你回答正确");}else{message.setText("你回答错误");}}catch(NumberFormatExceptionex){message.setText("请输入数字字符");}}textResult.requestFocus();validate();}}Teacher.javapubl
8、icclassTeacher{intnumberOne,numberTwo;Stringoperator="";booleanright;publicintgiveNumberOne(intn){numberOne=(int)(Math.random()*n);//生成n以内的整数returnnumberOne;}publicin
此文档下载收益归作者所有