查看: 1935|回复: 0

[Java代码] 基于java swing实现答题系统

发表于 2018-2-25 08:00:00

本文实例为大家分享了java swing实现答题系统的具体代码,供大家参考,具体内容如下

如上图所示,代码亲测可用,如下:

  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JTextField;
  16. /**
  17. * 2017-01-09
  18. * @author Koow
  19. *
  20. */
  21. public class sa extends JFrame implements ActionListener, KeyListener {
  22. private JLabel title = null;
  23. private JButton submit = null;
  24. private JPanel center = null;
  25. // items用来跳转
  26. private int item1 = 0;
  27. private int item2 = 1;
  28. private int item3 = 2;
  29. private int item4 = 3;
  30. private JLabel[] FormulaLabels; // 用来存放表达式的各个项
  31. private List<String> allResult; // 用来存放随机出来的结果
  32. private JTextField[] userResults; // 用来让用户输入结果
  33. private JLabel[] judge; // 用来显示图标,以显示用户输入的结果是否正确
  34. private List<String> inputResults = null; // 可以不要
  35. private JTextField scoreField = null;
  36. private int textCount = 1; // 用来记录当前焦点的位置
  37. public static void main(String[] args){
  38. sa s=new sa();
  39. }
  40. public sa() {
  41. // TODO Auto-generated constructor stub
  42. inputResults = new ArrayList<String>();
  43. // 实例化
  44. title = new JLabel("题目很简单,认真答哦!~(每题1分,答对加1分,答错不扣分!)");
  45. submit = new JButton("提交");
  46. submit.addActionListener(this);
  47. GridLayout layout = new GridLayout(21, 6);
  48. center = new JPanel(layout);
  49. // 调用方法实例化方法
  50. initLables();
  51. initUserRestult();
  52. initJudge();
  53. // 将各项填入到center面板(GridLayout)
  54. int count = 1;
  55. int formulaCount = 0;
  56. int userResultsCount = 0;
  57. int judgeCount = 0;
  58. while (count <= 120) {
  59. // 判断count%6,确定要填哪个组件
  60. if (count % 6 == 1 || count % 6 == 2 || count % 6 == 3 || count % 6 == 4) {
  61. center.add(FormulaLabels[formulaCount]);
  62. formulaCount++;
  63. } else if (count % 6 == 5) {
  64. center.add(userResults[userResultsCount]);
  65. userResultsCount++;
  66. } else if (count % 6 == 0) {
  67. center.add(judge[judgeCount]);
  68. judgeCount++;
  69. }
  70. count++;
  71. }
  72. center.add(new JLabel());
  73. center.add(new JLabel());
  74. center.add(new JLabel("总分:"));
  75. scoreField = new JTextField();
  76. scoreField.setEditable(false);
  77. center.add(scoreField);
  78. center.add(new JLabel());
  79. center.add(new JLabel());
  80. this.add(center, BorderLayout.CENTER);
  81. // 添加组件
  82. this.add(submit, BorderLayout.SOUTH);
  83. this.add(title, BorderLayout.NORTH);
  84. // 显示
  85. this.setLocation(400, 10);
  86. this.setVisible(true);
  87. this.setSize(500, 700);
  88. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  89. this.setTitle("简单测试系统");
  90. try {
  91. Thread.sleep(500);
  92. } catch (InterruptedException e) {
  93. // TODO Auto-generated catch block
  94. e.printStackTrace();
  95. }
  96. userResults[0].requestFocus();
  97. }
  98. /**
  99. * 这个方法用来初始化算式各项的数组
  100. */
  101. private void initLables() {
  102. int sum;
  103. int firstNumber;
  104. int secondNumber;
  105. allResult = new ArrayList<String>();
  106. // 建立算式标签
  107. this.FormulaLabels = new JLabel[80];
  108. // 将数组中的每一个标签都实例化
  109. for (int i = 0; i < 80; i++) {
  110. FormulaLabels[i] = new JLabel();
  111. }
  112. // 将表达式各项赋值内容,并将随机数的和算出结果存入Arraylist中,以便后期核对结果使用
  113. while (item4 < 80) {
  114. firstNumber = new Random().nextInt(99) + 1;
  115. FormulaLabels[item1].setText(String.valueOf(firstNumber));
  116. FormulaLabels[item2].setText("+");
  117. secondNumber = new Random().nextInt(99) + 1;
  118. FormulaLabels[item3].setText(String.valueOf(secondNumber));
  119. FormulaLabels[item4].setText("=");
  120. sum = firstNumber + secondNumber;
  121. allResult.add(String.valueOf(sum));
  122. item1 += 4;
  123. item2 += 4;
  124. item3 += 4;
  125. item4 += 4;
  126. }
  127. // for(int i=0;i<80;i++){
  128. // System.out.println(FormulaLabels[i].getText());
  129. // }
  130. }
  131. /**
  132. * 这个方法用来实例化userResults数组,并实例化里面的每一个JTextField
  133. */
  134. private void initUserRestult() {
  135. userResults = new JTextField[20];
  136. for (int i = 0; i < 20; i++) {
  137. userResults[i] = new JTextField();
  138. userResults[i].setSize(20, 20);
  139. // 后加的监听
  140. userResults[i].addKeyListener(this);
  141. }
  142. }
  143. /**
  144. * 这个方法用来实例化judge数组,并将里面的每一个Label实例化
  145. */
  146. private void initJudge() {
  147. judge = new JLabel[20];
  148. for (int i = 0; i < 20; i++) {
  149. judge[i] = new JLabel("好好算呦");
  150. }
  151. }
  152. // 实现监听方法,当用户点击提交后要做的事情
  153. @Override
  154. public void actionPerformed(ActionEvent arg0) {
  155. // TODO Auto-generated method stub
  156. int score = 0;
  157. for (int i = 0; i < 20; i++) {
  158. // 将用户输入的结果全部获取,存入一个ArrayList中,可以不要
  159. inputResults.add(userResults[i].getText().toString().trim());
  160. System.out.println(userResults[i].getText().toString());
  161. // 取出来用户输入的答案和之前算好的结果去比较,不对就把后面的标签设置为回答错误,
  162. // 如果正确就设置为回答正确
  163. String result = userResults[i].getText().toString().trim();
  164. if (result.equals(allResult.get(i))) {
  165. judge[i].setText("回答正确");
  166. judge[i].setForeground(Color.RED);
  167. score++;
  168. } else {
  169. judge[i].setText("回答错误");
  170. judge[i].setForeground(Color.GREEN);
  171. }
  172. }
  173. scoreField.setText(String.valueOf(score));
  174. }
  175. @Override
  176. public void keyPressed(KeyEvent e) {
  177. // TODO Auto-generated method stub
  178. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  179. userResults[textCount].requestFocus();
  180. if (textCount < 19) {
  181. textCount++;
  182. }
  183. }
  184. }
  185. @Override
  186. public void keyTyped(KeyEvent e) {
  187. // TODO Auto-generated method stub
  188. }
  189. @Override
  190. public void keyReleased(KeyEvent e) {
  191. // TODO Auto-generated method stub
  192. }
  193. }
复制代码

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报