简体   繁体   English

比较单选按钮的文本和数据库字段

[英]Comparing radio button's text and a database field

I'm preparing a test page in java and and questions are coming from database but when user choose a radio button and click finish button, cant see his score. 我正在用Java准备测试页,并且问题来自数据库,但是当用户选择一个单选按钮并单击“完成”按钮时,看不到他的分数。

So program should compare radio button's text and right option field which come from database.(Just last question is working good because i wrote it myself. its not from database.) 因此程序应该比较来自数据库的单选按钮的文本和正确的选项字段。(最后一个问题很好用,因为我自己写了它。它不是来自数据库。)

I think i have problem on radio button item listener class but i couldnt find solution. 我认为单选按钮项侦听器类存在问题,但找不到解决方案。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class ExamSystem extends JFrame {

    public JLabel q1,q2;

    public JButton ok;

    public JRadioButton a1,a2,a3,a4,b1,b2,b3,b4;

    public JPanel pnl1,pnl2,pnlchoices1,pnlchoices2;

    public ButtonGroup grp1,grp2,grp3,grp4,grp5;

    public int score=0;

    public Connection con;

    public Statement st;

    public ResultSet rs;

    public ExamSystem () {
        super("Exam");
        Container container = getContentPane();
        container.setLayout(new GridLayout(11,1));

        pnl1 = new JPanel();
        pnl1.setLayout(new GridLayout(1,1));

        q1 = new JLabel();
        pnl1.add(q1);

        pnlchoices1 = new JPanel();
        pnlchoices1.setLayout(new GridLayout(2,2));    
        a1 = new JRadioButton();
        a2 = new JRadioButton();
        a3 = new JRadioButton();
        a4 = new JRadioButton();
        pnlchoices1.add(a1);
        pnlchoices1.add(a2);
        pnlchoices1.add(a3);
        pnlchoices1.add(a4);
        container.add(pnl1);

        container.add(pnlchoices1);
        grp1 = new ButtonGroup();
        grp1.add(a1);
        grp1.add(a2);
        grp1.add(a3);
        grp1.add(a4);

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.print("sürücü yüklendi");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/etest", "root", "1234");
            st = con.createStatement();

            String sql = "Select * from questions";
            rs = st.executeQuery(sql);


            //int id_col = rs.getInt("sid");
            //String id = Integer.toString(id_col);
            while ( rs.next()) {
                String soru = rs.getString("question");
                String birinci = rs.getString("first");
                String ikinci = rs.getString("second");
                String ucuncu = rs.getString("third");
                String dorduncu = rs.getString("forth");
                String dogru = rs.getString("right");

                q1.setText(soru);
                a1.setText(birinci);
                a2.setText(ikinci);
                a3.setText(ucuncu);
                a4.setText(dorduncu);

                pnl1 = new JPanel();
                pnl1.setLayout(new GridLayout(1,1));

                q1 = new JLabel();
                pnl1.add(q1);
                pnlchoices1 = new JPanel();
                pnlchoices1.setLayout(new GridLayout(2,2));  
                a1 = new JRadioButton();
                a2 = new JRadioButton();
                a3 = new JRadioButton();
                a4 = new JRadioButton();

                pnlchoices1.add(a1);
                pnlchoices1.add(a2);
                pnlchoices1.add(a3);
                pnlchoices1.add(a4);

                container.add(pnl1);

                container.add(pnlchoices1);
                grp1 = new ButtonGroup();
                grp1.add(a1);
                grp1.add(a2);
                grp1.add(a3);
                grp1.add(a4);
            }
        }
        catch (Exception s)
        {
            System.out.print(s.getMessage());
        } 

        pnl2 = new JPanel();
        pnl2.setLayout(new GridLayout(1,1));

        q2 = new JLabel(" Which one is biggest?");
        pnl2.add(q2);

        pnlchoices2 = new JPanel();
        pnlchoices2.setLayout(new GridLayout(2,2));    
        b1 = new JRadioButton(" 1");
        b2 = new JRadioButton(" 2");
        b3 = new JRadioButton(" 3");
        b4 = new JRadioButton(" 4");
        pnlchoices2.add(b1);
        pnlchoices2.add(b2);
        pnlchoices2.add(b3);
        pnlchoices2.add(b4);

        container.add(pnl2);
        container.add(pnlchoices2);

        ok = new JButton("Finish");
        ok.setBackground(Color.RED);
        container.add(ok);

        setSize(500,500);
        setVisible(true);

        RadioButtonHandler handler = new RadioButtonHandler();
        a1.addItemListener(handler);
        a2.addItemListener(handler);
        a3.addItemListener(handler);
        a4.addItemListener(handler);
        b1.addItemListener(handler);
        b2.addItemListener(handler);
        b3.addItemListener(handler);
        b4.addItemListener(handler);

        grp2 = new ButtonGroup();
        grp2.add(b1);
        grp2.add(b2);
        grp2.add(b3);
        grp2.add(b4);

        ButtonHandler btnHandler = new ButtonHandler();
        ok.addActionListener(btnHandler);
    }

    public static void main(String[]args) {
        ExamSystem application = new ExamSystem();
        application.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public class RadioButtonHandler implements ItemListener {
        public void itemStateChanged(ItemEvent event) {
            if (event.getSource()==b4) 
                score++;
        }   
    }

    public class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource()==ok) {
                JOptionPane.showMessageDialog(null,"Score: " +score,"",JOptionPane.INFORMATION_MESSAGE);
                //dispose();
            }
        }
    }
}

There are a number of problems with your code, but the most significant problem, and the one that means the exam system isn't calculating the total score, is that you only increment the score when the last radio button changes state. 您的代码有很多问题,但是最严重的问题是,考试系统没有计算总分,这是您仅在最后一个单选按钮更改状态时才增加分数。 You can select correct or incorrect answers for the other questions, but as your button handler only recognises the radio button b4 , this is the only radio button that will affect the score. 您可以为其他问题选择正确或不正确的答案,但是由于按钮处理程序仅识别单选按钮b4 ,因此这是唯一会影响得分的单选按钮。

(Incidentally, you also increment the score when the 4 radio button is deselected , so you can make the score go as high as you like by repeatedly clicking the 4 and 2 radio buttons, for example.) (顺便说一句,例如,当取消选中 4单选按钮时,您也会增加分数,因此,例如,可以通过反复单击42单选按钮来使分数达到想要的最高水平。)

In order to fix it, there are a number of changes I would recommend making. 为了修复它,我建议进行一些更改。

Firstly, I wouldn't recommend trying to keep track of the score as you go along. 首先,我不建议您在进行过程中尝试跟踪得分。 Instead, when you click the Finish button, run through a list of 'correct' buttons and see how many of them are checked (use the isSelected() method for this). 相反,当您单击“完成”按钮时,遍历“正确”按钮的列表并查看其中有多少个被选中(为此使用isSelected()方法)。 Add a field named correctButtons , and declare it to be of type List<JRadioButton> , ie 添加一个名为correctButtons的字段,并将其声明为List<JRadioButton>类型,即

private List<JRadioButton> correctButtons;

We'll populate this list before the while loop, but before we can add to the list, we need a list to add to. 我们将在while循环之前填充此列表,但是在添加到列表之前,需要添加一个列表。 Add the following line above the loop: 在循环上方添加以下行:

correctButtons = new ArrayList<JRadioButton>();

You will also need to add the lines 您还需要添加行

import java.util.List;
import java.util.ArrayList;

at the top of your file. 在文件的顶部。

There is also an issue with your code generating a blank set of radio buttons. 您的代码生成一组空白的单选按钮时也存在问题。 This is because you create one set of radio buttons before the while loop and once each time through the loop. 这是因为您在while循环之前创建了一组单选按钮,并且每次循环创建了一次。 Inside this loop, you set the labels of the last set of radio buttons and create a new set. 在此循环中,设置最后一组单选按钮的标签并创建新的单选按钮。 Of course, the last set of buttons will never have any labels set. 当然,最后一组按钮将永远不会设置任何标签。 It would be better to only create the sets of radio buttons as you need them, inside the loop. 最好在循环内仅根据需要创建单选按钮集。 Get rid of all this code from outside the while loop: while循环之外删除所有这些代码:

    pnl1 = new JPanel();
    pnl1.setLayout(new GridLayout(1,1));

    q1 = new JLabel();

    // ... rest of code omitted, until

    grp1.add(a3);
    grp1.add(a4);

Inside the while loop, move the calls to q1.setText() and so on further down, after the labels and radio buttons have been created. 在创建标签和单选按钮之后,在while循环内,将调用移至q1.setText()等,然后进一步向下移动。

At the end of the loop, we will need to figure out which answer was correct and add the corresponding button to our list of correct buttons. 在循环的最后,我们将需要找出哪个答案是正确的,并将相应的按钮添加到我们的正确按钮列表中。

The while loop should then look like the following: while循环应如下所示:

        while ( rs.next()) {
            String soru = rs.getString("question");
            String birinci = rs.getString("first");
            String ikinci = rs.getString("second");
            String ucuncu = rs.getString("third");
            String dorduncu = rs.getString("forth");
            String dogru = rs.getString("right");

            // Calls to q1.setText, a1.setText, etc. moved from here.

            pnl1 = new JPanel();
            pnl1.setLayout(new GridLayout(1,1));

            q1 = new JLabel();
            pnl1.add(q1);
            pnlchoices1 = new JPanel();
            pnlchoices1.setLayout(new GridLayout(2,2));  
            a1 = new JRadioButton();
            a2 = new JRadioButton();
            a3 = new JRadioButton();
            a4 = new JRadioButton();

            pnlchoices1.add(a1);
            pnlchoices1.add(a2);
            pnlchoices1.add(a3);
            pnlchoices1.add(a4);

            container.add(pnl1);

            container.add(pnlchoices1);
            grp1 = new ButtonGroup();
            grp1.add(a1);
            grp1.add(a2);
            grp1.add(a3);
            grp1.add(a4);

            // Calls to q1.setText etc moved here.
            q1.setText(soru);
            a1.setText(birinci);
            a2.setText(ikinci);
            a3.setText(ucuncu);
            a4.setText(dorduncu);

            // Figure out which button is for the correct answer
            // and add it to our list of correct buttons.
            if (dogru.equals(birinci)) {
                correctButtons.add(a1);
            } else if (dogru.equals(ikinci)) {
                correctButtons.add(a2);
            } else if (dogru.equals(ucuncu)) {
                correctButtons.add(a3);
            } else if (dogru.equals(dorduncu)) {
                correctButtons.add(a4);
            } else {
                // If we get here, the correct answer is not one of the
                // options.  I don't know how you want to handle this.
            }
        }

As the radio button b4 is for another correct answer, you may want to add that to correctButtons as well. 由于单选按钮b4是另一个正确答案,因此您可能还希望将其添加到correctButtons中。

Next, get rid of the class RadioButtonHandler and all of the calls to addItemListener that use it. 接下来,摆脱类RadioButtonHandler和使用它的对addItemListener所有调用。 We don't need it any more. 我们不再需要它了。

Finally, modify the ButtonHandler class's actionPerformed method , to calculate the score using the following code. 最后,修改ButtonHandler类的actionPerformed method ,以使用以下代码计算分数。 Add it immediately before the call to JOptionPane.showMessageDialog : 在调用JOptionPane.showMessageDialog之前立即添加它:

            int score = 0;
            for (JRadioButton correctButton : correctButtons) {
                if (correctButton.isSelected()) {
                    ++score;
                }
            }

I made these changes to your code, and it worked as I expected it to. 我对您的代码进行了这些更改,并且按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM