簡體   English   中英

比較單選按鈕的文本和數據庫字段

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

我正在用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();
            }
        }
    }
}

您的代碼有很多問題,但是最嚴重的問題是,考試系統沒有計算總分,這是您僅在最后一個單選按鈕更改狀態時才增加分數。 您可以為其他問題選擇正確或不正確的答案,但是由於按鈕處理程序僅識別單選按鈕b4 ,因此這是唯一會影響得分的單選按鈕。

(順便說一句,例如,當取消選中 4單選按鈕時,您也會增加分數,因此,例如,可以通過反復單擊42單選按鈕來使分數達到想要的最高水平。)

為了修復它,我建議進行一些更改。

首先,我不建議您在進行過程中嘗試跟蹤得分。 相反,當您單擊“完成”按鈕時,遍歷“正確”按鈕的列表並查看其中有多少個被選中(為此使用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;
                }
            }

我對您的代碼進行了這些更改,並且按預期運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM