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

时间:2014-04-17 18:13:32

标签: java database swing jdbc event-handling

我正在准备一个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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

(顺便提一下,当4单选按钮取消选择时,您也会增加分数,这样您可以反复点击{{1},让分数尽可能高。例如,和4单选按钮。)

为了解决这个问题,我建议做出一些改变。

首先,我建议您不要随时跟踪得分。相反,当您单击“完成”按钮时,请运行“正确”列表。按钮并查看其中有多少个被选中(使用isSelected()方法)。添加一个名为2的字段,并将其声明为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

我对您的代码进行了这些更改,并且它按照我的预期运行。