【问题标题】:Comparing radio button's text and a database field比较单选按钮的文本和数据库字段
【发布时间】:2014-04-17 18:13:31
【问题描述】:

我正在用 java 准备一个测试页面,问题来自数据库,但是当用户选择一个单选按钮并单击完成按钮时,看不到他的分数。

所以程序应该比较来自数据库的单选按钮的文本和正确的选项字段。(最后一个问题很好,因为我自己写的。它不是来自数据库。)

我认为我在单选按钮项目侦听器类上有问题,但我找不到解决方案。

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();
            }
        }
    }
}

【问题讨论】:

  • 您遇到任何错误或异常?
  • 没有任何错误或异常。它的工作,但分数没有增加。

标签: java database swing jdbc event-handling


【解决方案1】:

您的代码存在许多问题,但最重要的问题,即考试系统未计算总分的问题是,您仅在最后一个单选按钮更改状态时才增加分数。您可以为其他问题选择正确或错误的答案,但由于您的按钮处理程序仅识别单选按钮b4,因此这是唯一会影响分数的单选按钮。

(顺便说一句,当4 单选按钮取消选择时,您也会增加分数,因此您可以通过反复单击4 和@987654325 使分数尽可能高例如@单选按钮。)

为了修复它,我建议进行一些更改。

首先,我不建议您在进行过程中尝试跟踪分数。相反,当您单击“完成”按钮时,运行“正确”按钮列表并查看其中有多少被选中(为此使用isSelected() 方法)。添加一个名为correctButtons的字段,并将其声明为List<JRadioButton>类型,即

private List<JRadioButton> correctButtons;

我们将在while 循环之前填充此列表,但在我们可以添加到列表之前,我们需要一个要添加的列表。在循环上方添加以下行:

correctButtons = new ArrayList<JRadioButton>();

您还需要添加行

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

在文件的顶部。

生成一组空白单选按钮的代码也存在问题。这是因为您在 while 循环之前创建了一组单选按钮,并且每次循环一次。在这个循环中,您设置最后一组单选按钮的标签并创建一个新组。当然,最后一组按钮永远不会设置任何标签。最好只在循环内根据需要创建单选按钮集。从while 循环之外删除所有这些代码:

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

    q1 = new JLabel();

    // ... rest of code omitted, until

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

while 循环中,在创建标签和单选按钮之后,将调用移至q1.setText() 等等。

在循环结束时,我们需要确定哪个答案是正确的,并将相应的按钮添加到我们的正确按钮列表中。

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.
            }
        }

由于单选按钮b4 是另一个正确答案,您可能还想将其添加到correctButtons

接下来,删除类RadioButtonHandler 以及所有使用它的addItemListener 调用。我们不再需要它了。

最后,修改ButtonHandler类的actionPerformed method,使用以下代码计算分数。在调用JOptionPane.showMessageDialog之前立即添加:

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

我对您的代码进行了这些更改,它按我的预期工作。

【讨论】:

    猜你喜欢
    • 2014-02-12
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2014-01-20
    相关资源
    最近更新 更多