The examples in this article share with you the specific code of Java swing to implement the answering system for your reference. The specific content is as follows
As shown in the above figure, the code is available for personal testing, as follows:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /** * 2017-01-09 * @author Koow * */ public class sa extends JFrame implements ActionListener, KeyListener { private JLabel title = null; private JButton submit = null; private JPanel center = null; // items are used to jump private int item1 = 0; private int item2 = 1; private int item3 = 2; private int item4 = 3; private JLabel[] FormulaLabels; // Used to store various items of expressions private List<String> allResult; // Used to store random results private JTextField[] userResults; // Used to let the user enter results private JLabel[] judge; // Used to display icons to show whether the user input results are correct private List<String> inputResults = null; // You can do not private JTextField scoreField = null; private int textCount = 1; // Used to record the current focus public static void main(String[] args){ sa s=new sa(); } public sa() { // TODO Auto-generated constructor stub inputResults = new ArrayList<String>(); // Instantiated title = new JLabel("The question is very simple, answer it seriously! ~(1 point for each question, add 1 point for correct answer, no points will be deducted if the answer is wrong!)"); submit = new JButton("Submit"); submit.addActionListener(this); GridLayout layout = new GridLayout(21, 6); center = new JPanel(layout); // Call method instantiation method initLables(); initUserRestult(); initJudge(); // Fill in the center panel (GridLayout) int count = 1; int formulaCount = 0; int userResultsCount = 0; int judgeCount = 0; while (count <= 120) { // Determine count%6 and determine which component to fill if (count % 6 == 1 || count % 6 == 2 || count % 6 == 3 || count % 6 == 4) { center.add(FormulaLabels[formulaCount]); formulaCount++; } else if (count % 6 == 5) { center.add(userResults[userResultsCount]); userResultsCount++; } else if (count % 6 == 0) { center.add(judge[judgeCount]); judgeCount++; } count++; } center.add(new JLabel()); center.add(new JLabel()); center.add(new JLabel("Total Score:")); scoreField = new JTextField(); scoreField.setEditable(false); center.add(scoreField); center.add(new JLabel()); center.add(new JLabel()); this.add(center, BorderLayout.CENTER); // Add component this.add(submit, BorderLayout.SOUTH); this.add(title, BorderLayout.NORTH); // Show this.setLocation(400, 10); this.setVisible(true); this.setSize(500, 700); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Simple Test System"); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } userResults[0].requestFocus(); } /** * This method is used to initialize an array of various items of the formula*/ private void initLables() { int sum; int firstNumber; int secondNumber; allResult = new ArrayList<String>(); // Create the equation label this.FormulaLabels = new JLabel[80]; // Instance each label in the array for (int i = 0; i < 80; i++) { FormulaLabels[i] = new JLabel(); } // Assign the content of each item of the expression and store the sum of the random numbers into the Arraylist for later verification results while (item4 < 80) { firstNumber = new Random().nextInt(99) + 1; FormulaLabels[item1].setText(String.valueOf(firstNumber)); FormulaLabels[item2].setText("+"); secondNumber = new Random().nextInt(99) + 1; FormulaLabels[item3].setText(String.valueOf(secondNumber)); FormulaLabels[item4].setText("="); sum = firstNumber + secondNumber; allResult.add(String.valueOf(sum)); item1 += 4; item2 += 4; item3 += 4; item4 += 4; } // for(int i=0;i<80;i++){ // System.out.println(FormulaLabels[i].getText()); // } } /** * This method is used to instantiate the userResults array and instantiate each JTextField initUserRestult() { userResults = new JTextField[20]; for (int i = 0; i < 20; i++) { userResults[i] = new JTextField(); userResults[i].setSize(20, 20); // The listener added after userResults[i].addKeyListener(this); } } /** * This method is used to instantiate the judge array and instantiate each label in it*/ private void initJudge() { judge = new JLabel[20]; for (int i = 0; i < 20; i++) { judge[i] = new JLabel("Share it"); } } // Implement the listening method, what you want to do after the user clicks to submit @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub int score = 0; for (int i = 0; i < 20; i++) { // Get all the results entered by the user and save them into an ArrayList. You can do not inputResults.add(userResults[i].getText().toString().trim()); System.out.println(userResults[i].getText().toString()); // Take out the answer entered by the user and compare it with the previous calculated results. If it is wrong, set the subsequent label to the wrong answer. // If it is correct, set it to the correct answer String result = userResults[i].getText().toString().trim(); if (result.equals(allResult.get(i))) { judge[i].setText("Response is correct"); judge[i].setForeground(Color.RED); score++; } else { judge[i].setText("Error answer"); judge[i].setForeground(Color.GREEN); } } scoreField.setText(String.valueOf(score)); } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if (e.getKeyCode() == KeyEvent.VK_ENTER) { userResults[textCount].requestFocus(); if (textCount < 19) { textCount++; } } } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } }For more learning materials, please pay attention to the special topic "Management System Development".
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.