基于Java Swing实现的简易考试系统界面搭建

学习是我快乐

发布日期: 2020-06-05 19:54:26 浏览量: 184
评分:
star star star star star star star star star_border star_border
*转载请注明来自write-bug.com

一、任务介绍

1.1 任务描述

本任务要搭建一个考试系统界面。考试题型主要有两类:判断题和单选题,题库是存放在两个二维数组中,要求考试题目能随机生成5到题目,并能通过左边的树形结构选择相应题型来改变右边面板的内容,具体运行效果如下。

1.2 运行结果

主界面

判断题

单选题

二、任务目标

  • 学会分析“简易考试系统界面搭建”任务的实现思路

  • 根据思路独立完成“简易考试系统界面搭建”的源代码编写、编译和运行

  • 掌握Java动态生成组件的方法以及添加组件的方法

  • 掌握GUI开发过程中如何处理组件上发生的界面事件

三、实现思路

  • 窗口实现思路:

    设计产生随机数作为存放二维数组的下标,设置组件JLabel存放改下标中题目,设置组件JRadioButton存放选择选择项,将组件JRadioButton存放在组件ButtonGroup将选择像设置为单选。

  • 阅卷取消思路:
    利用ButtonGroup类中的clearSelection清空每组选择的选项,通过已知产生五组题目,即生成一个ButtonGroup[]的组件组,该组的长度为五,通过

    1. Enumeration<AbstractButton> radioBtns = buttonGroup[i].getElements();
    2. AbstractButton btn = radioBtns.nextElement();

    获得每一个ButtonGroup[]中的选择项与答案进行比较。【利用产生题目的随机数组定位答案进行判断】

四、实现代码及运行结果

My.java

  1. public interface My extends MouseListener, ActionListener {
  2. public void setJButton(JButton jButton[]);
  3. public void setJRadioButton(JRadioButton jRadioButton[]);
  4. public void setButtonGroup(ButtonGroup Group[]);
  5. public void setIntA(int a[]);
  6. public void setquestion_select(String string[][]);
  7. public void setquestion_judge(String string[][]);
  8. void setTree(JTree jTree);
  9. }

Police.java

  1. public class Police extends MouseAdapter implements My {
  2. JButton jButton[];
  3. JRadioButton jRadioButton[];
  4. ButtonGroup buttonGroup[];
  5. JTree jTree;
  6. int a[];
  7. String[][] question_select;
  8. String[][] question_judge;
  9. @Override
  10. public void actionPerformed(ActionEvent e) {
  11. double Mark = 0;
  12. if (e.getActionCommand().equals("确定")) {
  13. DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
  14. if (node.isLeaf()) {
  15. String s = node.getUserObject().toString().trim();
  16. if (s.equals("判断")) {
  17. for (int i = 0; i < a.length; i++) {
  18. Enumeration<AbstractButton> radioBtns = buttonGroup[i].getElements();
  19. AbstractButton btn = radioBtns.nextElement();
  20. System.out.println(question_select[a[i]][5]);
  21. System.out.println(btn.getText());
  22. if (question_select[a[i]][5].equals(btn.getText())) {
  23. Mark += 20;
  24. }
  25. buttonGroup[i].clearSelection();
  26. }
  27. JOptionPane.showMessageDialog(null, "成绩:" + Mark, "成绩", JOptionPane.WARNING_MESSAGE);
  28. } else {
  29. Mark = 0;
  30. for (int i = 0; i < a.length; i++) {
  31. Enumeration<AbstractButton> radioBtns = buttonGroup[i].getElements();
  32. AbstractButton btn = radioBtns.nextElement();
  33. System.out.println(question_judge[a[i]][3]);
  34. System.out.println(btn.getText());
  35. if (question_judge[a[i]][3].equals(btn.getText())) {
  36. Mark += 20;
  37. }
  38. buttonGroup[i].clearSelection();
  39. }
  40. JOptionPane.showMessageDialog(null, "成绩:" + Mark, "成绩", JOptionPane.WARNING_MESSAGE);
  41. }
  42. }
  43. } else {
  44. for (int i = 0; i < buttonGroup.length; i++) {
  45. buttonGroup[i].clearSelection();
  46. }
  47. }
  48. }

Masm_test.java

  1. public static void main(String[] args) {
  2. EventQueue.invokeLater(new Runnable() {
  3. public void run() {
  4. try {
  5. Masm_test window = new Masm_test();
  6. Police police = new Police();
  7. window.setMy(police);
  8. window.frame.setVisible(true);
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. });
  14. }
  15. /**
  16. * Create the application.
  17. */
  18. public Masm_test() {
  19. initialize();
  20. setVisible(true);
  21. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22. }
  23. /**
  24. * Initialize the contents of the frame.
  25. */
  26. private void initialize() {
  27. frame = new JFrame();
  28. frame.setBounds(100, 100, 609, 471);
  29. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. frame.getContentPane().setLayout(null);
  31. JPanel panel = new JPanel();
  32. panel.setBounds(0, 0, 594, 419);
  33. frame.getContentPane().add(panel);
  34. panel.setLayout(null);
  35. JSplitPane splitPane = new JSplitPane();
  36. splitPane.setBounds(0, 0, 594, 409);
  37. panel.add(splitPane);
  38. jButton = new JButton[2];
  39. jButton[0] = new JButton("确定");
  40. jButton[1] = new JButton("取消");
  41. jRadioButton = new JRadioButton[4];
  42. group = new ButtonGroup[6];
  43. tree = new JTree();
  44. tree.addMouseListener(new MouseAdapter() {
  45. @Override
  46. public void mouseClicked(MouseEvent e) {
  47. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
  48. if (node.isLeaf()) {
  49. String s = node.getUserObject().toString().trim();
  50. if (s.equals("判断")) {
  51. show(1);
  52. } else {
  53. show(0);
  54. }
  55. }
  56. }
  57. });
  58. tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("JTree") {
  59. {
  60. add(new DefaultMutableTreeNode("\u5224\u65AD"));
  61. add(new DefaultMutableTreeNode("\u5355\u9009"));
  62. }
  63. }));
  64. splitPane.setLeftComponent(tree);
  65. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  66. splitPane.setRightComponent(scrollPane);
  67. }
  68. void show(int n) {
  69. JPanel panel = new JPanel(new GridLayout(7, 1, 0, 0));
  70. a = new int[6];
  71. for (int i = 0; i < 6; i++) {
  72. a[i] = (int) (Math.random() * (10));
  73. for (int j = 0; j < i; j++) {
  74. if (a[j] == a[i]) {
  75. i--;
  76. }
  77. }
  78. }
  79. listener.setIntA(a);
  80. if (n == 0) {
  81. for (int i = 0; i < 6; i++) {
  82. JPanel p = new JPanel(new GridLayout(3, 1, 0, 0));
  83. lab = new JLabel(i + 1 + "、" + question_judge[a[i]][0]);
  84. // lab.addActionListener(listener);
  85. group[i] = new ButtonGroup();
  86. p.add(lab);
  87. for (int j = 0; j < 2; j++) {
  88. jRadioButton[j] = new JRadioButton(question_judge[a[i]][j + 1]);
  89. group[i].add(jRadioButton[j]);
  90. p.add(jRadioButton[j]);
  91. // jRadioButton[j].addActionListener(listener);
  92. }
  93. panel.add(p);
  94. }
  95. // scrollPane.setViewportView(panel);
  96. }
  97. if (n == 1) {
  98. // JPanel panel = new JPanel(new GridLayout(6, 1, 0, 0));
  99. for (int i = 0; i < 6; i++) {
  100. JPanel p = new JPanel(new GridLayout(5, 1, 0, 0));
  101. lab = new JLabel(i + 1 + "、" + question_select[a[i]][0]);
  102. // lab.addActionListener(listener);
  103. group[i] = new ButtonGroup();
  104. p.add(lab);
  105. for (int j = 0; j < jRadioButton.length; j++) {
  106. jRadioButton[j] = new JRadioButton(question_select[a[i]][j + 1]);
  107. group[i].add(jRadioButton[j]);
  108. p.add(jRadioButton[j]);
  109. // jRadioButton[j].addActionListener(listener);
  110. }
  111. panel.add(p);
  112. }
  113. }
  114. scrollPane.setViewportView(panel);
  115. JPanel panel_south = new JPanel();
  116. panel_south.add(jButton[0]);
  117. panel_south.add(jButton[1]);
  118. panel.add(panel_south);
  119. }
  120. void setMy(My listener) {
  121. this.listener = listener;
  122. for (int i = 0; i < jButton.length; i++) {
  123. jButton[i].addActionListener(listener);
  124. }
  125. listener.setButtonGroup(group);
  126. listener.setJButton(jButton);
  127. listener.setTree(tree);
  128. listener.setquestion_judge(question_judge);
  129. listener.setquestion_select(question_select);
  130. // listener.setJRadioButton(jRadioButton);
  131. }

登入首页

判断题

单选题

五、总结或感悟

通过实验三,实验四的对编写代码实现窗口,熟练掌握常用组件的常用方法的使用。本次实验结合Windowbuilder插件对组件布局很快的完成实现”出卷”;

根据实验提示选项单选的情况使用ButtonGroup存放选项radioBtns实现单选;根据提示一次出道五题想到获取每组ButtonGroup选择情况与答案进行对比实现“阅卷”。

结合三次实验,虽然在前两次实验中使用大量的时间学习如何布局,组件的使用,触发事件的过程,但是在第三次实验体现出了前两次实验的必要性;如何合理的封装程序,实现改变ButtonGroup[]的长度便改变题目的个数,以及将窗口可见的组件做成员变量,窗口不可见的做局部变量传入事件触发后的处理类(Police)中的方法。

上传的附件 cloud_download 报告and代码.rar ( 984.92kb, 3次下载 )
error_outline 下载需要11点积分

发送私信

43
文章数
9
评论数
最近文章
eject