Implements the Q & A System Based on java swing, and implements the Q & A System Using javaswing.
The examples in this article share the specific code of the java swing Q & A system for your reference. The specific content is as follows:
As shown in, the Code is available for test, 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. JPa Nel; 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 is used to jump to private int item1 = 0; private int item2 = 1; private int item3 = 2; private int item4 = 3; private JLabel [] FormulaLabels; // used to store the private List of each item of the expression <String> allResult; // used to store random results. private JTextField [] userResults; // used to allow users to enter private JLabel [] judge results; // used to display icons, private List <String> inputResults = null; // do not use private JTextField scoreField = null; private int textCount = 1; // used to record the current focus position public static void main (String [] args) {sa s = new sa ();} public sa () {// TODO Auto-generated constructor stub inputResults = new ArrayList <String> (); // instantiate title = new JLabel ("The question is very simple. Answer it carefully !~ (1 point for each question, 1 point for correct answers, no points for wrong answers !) "); Submit = new JButton (" submit "); submit. addActionListener (this); GridLayout layout = new GridLayout (21, 6); center = new JPanel (layout); // call the method instantiation method initLables (); initUserRestult (); initJudge (); // fill in items to the center panel (GridLayout) int count = 1; int formulaCount = 0; int userResultsCount = 0; int judgeCount = 0; while (count <= 120) {// judge count % 6, determine which component to fill in 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 the component this. add (submit, BorderLayout. SOUTH); this. add (title, BorderLayout. NORTH); // display this. setLocation (400, 10); this. setVisible (true); this. setSize (500,700); this. setdefaclocloseoperation (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 the array of formula items */private void initLables () {int sum; int firstNumber; int secondNumber; allResult = new ArrayList <String> (); // create the formula label this. formulaLabels = new JLabel [80]; // instantiate each label in the array for (int I = 0; I <80; I ++) {FormulaLabels [I] = new JLabel () ;}// assign values to each expression and save the random number sum and calculation results to the Arraylist, for later verification results, use 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 in it */private void initUserRestult () {userResults = new JTextField [20]; for (int I = 0; I <20; I ++) {userResults [I] = new JTextField (); userResults [I]. setSize (20, 20); // The added listener 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 ("good luck") ;}/// listener method, what you need to do after clicking submit @ Override public void actionreceivmed (ActionEvent arg0) {// TODO Auto-generated method stub int score = 0; for (int I = 0; I <20; I ++) {// obtain all the user input results and store them in an ArrayList. Do not use inputResults. add (userResults [I]. getText (). toString (). trim (); System. out. println (userResults [I]. getText (). toString (); // compare the answer entered by the user with the previously calculated result. If it is incorrect, set the following tag as an answer error, // if the answer is correct, set String result = userResults [I]. getText (). toString (). trim (); if (result. equals (allResult. get (I) {judge [I]. setText ("correct answer"); judge [I]. setForeground (Color. RED); score ++;} else {judge [I]. setText ("Answer error"); 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 information, see management system development.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.