青岛理工大学-实验4:图形用户界面设计(共7页).doc
精选优质文档-倾情为你奉上课程实验报告课程名称Java应用技术班级计算112实验日期2013.10.24姓名学号实验成绩实验名称实验4:图形用户界面设计实验目的及要求1掌握窗口和菜单的使用;2掌握文本框、按钮的使用、标签等常用图形组件的使用;3掌握面板和画布的使用及布局设计;4掌握常用的窗口事件、鼠标事件、焦点事件、键盘事件。实验环境操作系统:WindowsIDE:JDK 或者 MyEclipse实验内容(1)考试题。要求在一个界面内至少显示5道选择题,每道题4个选项。另有2个命令按钮,分别为“重新答题”(全部选项及正确答题数清空)和“提交”(计算),在按钮下方的标签内显示最终的正确的题目数。界面大致布局如下:题目1:XXXXXXXX正确的答题数:XXXXXXXXAXXXBXXXCXXXDXXX题目2:XXXXXXXXAXXX BXXX CXXX DXXX提交重新答题调试过程及实验结果实验中遇到最主要的问题就是类掌握的不熟练,对相应的方法不知道。解决的方法:查找丰富的网络资源和API文档。一下是调试过程:1、初始界面:答题后的界面:重新答题则恢复到原来的情况。总结实验不足的地方就是:它没有一个好的模式去适应不同的题目和问题。我所做的东西全部都是僵硬化的。我想如果他能数据库连接起来,应该会有更大的灵活性。附录import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JRadioButton;import javax.swing.ButtonGroup;import javax.swing.JFrame;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.event.*;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Toolkit;import javax.swing.JButton;import java.awt.event.*;public class Examination extends JFrameString qustion = new String"1.管锥编是谁的著作?" ,"2.CPU的全称是什么?" ,"3.美国的首都在哪儿?","4.儒家学术的创始人是谁?" ,"5.西游记是我国何人写著而成?"String chooser = new String "A.钱钟书","B.杨绛","C.纪晓岚","D.巴金", "A.中央处理器","B.computer","C.come pu","D.cpu", "A.华盛顿","B.纽约","C.加州","D.旧金山", "A.孔子","B.孟子","C.老子","D.荀子", "A.吴承恩","B.罗贯中","C.曹雪芹","D.施耐庵"ProduceExam produceExam = new ProduceExam5;/统计选择正确的答案的个数,重复选择的不算private int totalture = 0;private JLabel endScore = new JLabel("你一共答对题目:");private JButton endExam = new JButton("提交");private JButton restart = new JButton("重新答题");private JPanel jp = new JPanel(new GridLayout(1,1);public Examination(String name)throws Exceptionsuper(name);/屏幕居中放置setLocationRelativeTo(null);setLayout(new GridLayout(7,1);for(int i=0;i<5;i+)produceExami = new ProduceExam(this.qustioni,chooseri);this.add(produceExami);jp.add(endExam);jp.add(restart);this.add(endScore);this.add(jp);endExam.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)String str = e.getActionCommand();if(str = "提交")if(JOptionPane.showConfirmDialog(null, "你确定要提交答案吗?") = JOptionPane.OK_OPTION)endScore.setText("你一共答对题目:" + totalture +"道"););restart.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)totalture = 0;endScore.setText("你一共答对题目: ");for(int i=0;i<5;i+)produceExami.reset(););public static void main(String args)throws ExceptionExamination kaoshi = new Examination("考试");/设置成windows风格UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");SwingUtilities.updateComponentTreeUI(kaoshi.getRootPane();kaoshi.setVisible(true);kaoshi.pack();kaoshi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/内部类,用于产生题目和选项class ProduceExam extends JPanel/显示题目private JLabel qustion;/问题选项private JRadioButton chooser = new JRadioButton4;/把问题选项用ButtonGroup包围起来private ButtonGroup bagChooser = new ButtonGroup();/带参构造函数 public ProduceExam(String qustion, String chooser) /设置布局管理器 setLayout(new GridLayout(2,1); this.qustion = new JLabel(qustion); this.add(this.qustion); JPanel jp = new JPanel(new GridLayout(1,1); for(int i=0;i<4;i+) this.chooseri = new JRadioButton(chooseri); this.bagChooser.add(this.chooseri); jp.add(this.chooseri); this.add(jp); /为第一个JRadioButton添加监听器,因为第一个答案是正确的 this.chooser0.addItemListener(new ItemListener() public void itemStateChanged(ItemEvent e) if(ItemEvent.SELECTED = e.getStateChange() ) totalture+; ); public void reset() /清除选中状态 bagChooser.clearSelection(); 专心-专注-专业