gpt4 book ai didi

java - 将所选选项与一组单选按钮中的正确答案进行比较

转载 作者:行者123 更新时间:2023-11-30 04:19:30 25 4
gpt4 key购买 nike

如何从一组单选按钮中获取所选选项。有哪些可能的方法来检查所选选项的正确答案?(我是初学者)这是正在处理的代码。仅包含 2 个问题,每个问题有 4 个单选按钮,它们已分组。现在我必须在检查正确答案后显示分数

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MyRadioButton {

private JFrame frame = new JFrame("RadioRadio");
private JLabel timerl = new JLabel("Press Button to start");
private JPanel butp = new JPanel();
private JPanel panel1=new JPanel();
private JLabel q1=new JLabel("Qn 1:Who is the first President Of India?");
private JLabel q2=new JLabel("Qn 2:Who is the first Prime Minister Of India?");

private JPanel panel2=new JPanel();
private JPanel panel3=new JPanel();
private JLabel label=new JLabel();
private JButton button = new JButton("Start Exam");
private JRadioButton rd1=new JRadioButton("a)Nehru");
private JRadioButton rd2=new JRadioButton("b)R.Prasad");
private JRadioButton rd3=new JRadioButton("c)S.R.Krishnan");
private JRadioButton rd4=new JRadioButton("d)GV.Mavlankar");
private JRadioButton rd5=new JRadioButton("a)Nehru");
private JRadioButton rd6=new JRadioButton("b)R.Prasad");
private JRadioButton rd7=new JRadioButton("c)S.R.Krishnan");
private JRadioButton rd8=new JRadioButton("d)GV.Mavlankar");

ButtonGroup group1 = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();

public int result=0;
private Timer mytimer;
private String ss = "Time Remaining %02d Seconds!";
private int elapsedSeconds = 0;
private int total = 60;
JLayeredPane pane=new JLayeredPane();


public MyRadioButton() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


if (mytimer != null && mytimer.isRunning()) {
//String t = String.format("Result:Your Score is %d", result);
//label.setText(t);
mytimer.stop();
elapsedSeconds = 0;
timerl.setText("Exam Terminated");
} else {
panel1.setBounds(0, 100, 500, 100);
panel1.add(q1);
pane.add(panel1,pane.DEFAULT_LAYER);
panel2.setBounds(0,200,500,100);
panel2.add(q2);
pane.add(panel2,pane.DEFAULT_LAYER);
panel3.setBounds(0, 300, 500, 100);
pane.add(panel3,pane.DEFAULT_LAYER);
panel3.add(label);


panel1.add(rd1);
panel1.add(rd2);
panel1.add(rd3);
panel1.add(rd4);
panel2.add(rd5);
panel2.add(rd6);
panel2.add(rd7);
panel2.add(rd8);
group1.add(rd1);
group1.add(rd2);
group1.add(rd3);
group1.add(rd4);
group2.add(rd5);
group2.add(rd6);
group2.add(rd7);
group2.add(rd8);
mytimer = new Timer(1000, new TimerListener());
mytimer.start();
String t = String.format(ss, total);
timerl.setText(t);

}
}
});

frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.add(pane);
butp.setBounds(0,0,500,100);
butp.add(button);
butp.add(timerl);
pane.add(butp,pane.DEFAULT_LAYER);
frame.setVisible(true);
}

private class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
elapsedSeconds++;
if (elapsedSeconds == total) {
mytimer.stop();
elapsedSeconds = 0;
timerl.setText("Time Up");
} else {
String t = String.format(ss, total - elapsedSeconds);
timerl.setText(t);
}
}
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MyRadioButton r = new MyRadioButton();

}
});
}
}

最佳答案

您可以向每个按钮添加一个 ActionListener,您可以在其中设置,例如,将类字段设置为当前选定的单选按钮值。

...
String firstAnswer;
String secondAnswer;
...

private FirstQuestionActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
firstAnswer = e.getActionCommand();
}
}

private SecondQuestionActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
secondAnswer = e.getActionCommand();
}
}

并以这种方式使用它:rb1.addActionListener(new FirstQuestionActionListener());

关于java - 将所选选项与一组单选按钮中的正确答案进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17444840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com