Java-在radioButtons和JPanel中使用IsSelected函数
我必须用Java设置一个“测验”,使用Java-在radioButtons和JPanel中使用IsSelected函数,java,swing,jbutton,buttongroup,Java,Swing,Jbutton,Buttongroup,我必须用Java设置一个“测验”,使用JPanel和JRadioButton 我的代码首先从一个文件中读取一个文本,然后把它放在一个面板上,面板上有单选按钮、按钮组和面板。到目前为止,一切顺利 然而,当我必须从用户那里收集数据并与正确答案进行比较时,我的问题就开始了 我有课堂问题,答案和阅读(除了主要的和显示在面板上) 我的想法是,当用户单击submit按钮时,代码开始检查以进行测验。但是,当我在打开的窗口上单击它时,什么也没有发生 我将数据收集到ArrayList中进行比较 checkquic
JPanel
和JRadioButton
我的代码首先从一个文件中读取一个文本,然后把它放在一个面板上,面板上有单选按钮、按钮组和面板。到目前为止,一切顺利
然而,当我必须从用户那里收集数据并与正确答案进行比较时,我的问题就开始了
我有课堂问题,答案和阅读(除了主要的和显示在面板上)
我的想法是,当用户单击submit
按钮时,代码开始检查以进行测验。但是,当我在打开的窗口上单击它时,什么也没有发生
我将数据收集到ArrayList
中进行比较
checkquick
方法应该在用户提交测验后调用。它将单选按钮选择的文本与答案进行比较。该程序应该是“运行”,直到用户点击“提交”按钮,但我相信它不会发生
我试图在代码中使用ActionListener
,但无法将用户的选择与我使用的数据进行比较
很抱歉代码太多。我试图发布一个mcve问题。据我所知,代码是可编译的,但我不能再忽略任何行
谢谢你的帮助,代码如下。
Class DisplayOnPanel为要使用的子类创建一个框架:
import javax.swing.JFrame;
public class DisplayOnPanel extends JFrame {
JFrame frame = new JFrame();
public DisplayOnPanel(){
frame.setSize(500,500);
}
}
public class Main {
public static void main (String[]args){
new Read();
}
}
public class Question {
private String _question;
private String _option1;
private String _option2;
private String _option3;
private String _option4;
private int _qIndex=0;
public Question(String question, String option1, String option2, String option3,
String option4){
this._question = question;
this._option1 = option1;
this._option2 = option2;
this._option3 = option3;
this._option4 = option4;
}
public void set_qIndex(int index) {
this._qIndex = index;
}
主类:
import javax.swing.JFrame;
public class DisplayOnPanel extends JFrame {
JFrame frame = new JFrame();
public DisplayOnPanel(){
frame.setSize(500,500);
}
}
public class Main {
public static void main (String[]args){
new Read();
}
}
public class Question {
private String _question;
private String _option1;
private String _option2;
private String _option3;
private String _option4;
private int _qIndex=0;
public Question(String question, String option1, String option2, String option3,
String option4){
this._question = question;
this._option1 = option1;
this._option2 = option2;
this._option3 = option3;
this._option4 = option4;
}
public void set_qIndex(int index) {
this._qIndex = index;
}
Clsas问题:
import javax.swing.JFrame;
public class DisplayOnPanel extends JFrame {
JFrame frame = new JFrame();
public DisplayOnPanel(){
frame.setSize(500,500);
}
}
public class Main {
public static void main (String[]args){
new Read();
}
}
public class Question {
private String _question;
private String _option1;
private String _option2;
private String _option3;
private String _option4;
private int _qIndex=0;
public Question(String question, String option1, String option2, String option3,
String option4){
this._question = question;
this._option1 = option1;
this._option2 = option2;
this._option3 = option3;
this._option4 = option4;
}
public void set_qIndex(int index) {
this._qIndex = index;
}
类读取-读取文件,然后在面板上显示结果。它还用答案、问题、按钮组和JPanel填充数组列表
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Read extends DisplayOnPanel {
protected ArrayList<Question> Questions = new ArrayList<>();
protected ArrayList<ButtonGroup> BG = new ArrayList<>();
protected ArrayList<JPanel> JP = new ArrayList<>();
protected ArrayList<Answer> Answers = new ArrayList<>();
protected int qNumber = 0, finalscore = 0;
private JLabel lblScore;
private JToggleButton Submit = new JToggleButton();
//constructor
public Read() {
super();
//a "label" in the file will indicate the final score
final int NUMBER_OF_LABELS_ADDED_TO_FRAME = 1;
int number_of_lines_in_the_file, final_score = 0;
try {
number_of_lines_in_the_file = read(Questions);
addButtonsToFrame(Questions, number_of_lines_in_the_file +
NUMBER_OF_LABELS_ADDED_TO_FRAME, BG, JP);
Submit.setText("Submit Quiz"); //create a submit button
JPanel SubmitPanel = new JPanel();
SubmitPanel.setVisible(true);
this.add(SubmitPanel);
SubmitPanel.add(Submit);
Submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
for (int i = 0; i < BG.size(); i++) {
//handle cases of the user didn't complete the test
if (BG.get(i).getSelection()==null) {
JOptionPane.showMessageDialog(frame, "Exam not finished - Restart.");
i=BG.size()-1; //finish the loop
frame.setVisible(false);
dispose();
new Read();
}
}
checkQuiz(BG, Answers, ONE_QUESTION_GRADE); //check quiz
Submit.setEnabled(false); //can't redo quiz //
// unless "Restart" pressed
}
});
//adding final score label
lblScore = new JLabel("Your Final Score: " + finalscore);
add(lblScore);
pack();
this.setVisible(true);
while (!Submit.isSelected()) {
if (Submit.isSelected()) {
checkQuiz(BG, Answers);
}
}
} catch (FileNotFoundException e) {
System.out.println("couldn't open file");
}
}
//the method reads from the file
//returns the number of lines (quesiton) in the file
public int read(ArrayList<Question> Questions) throws FileNotFoundException {
int number_of_lines = 0;
try {
File f = new File("C:\\Users\\Assaf\\Desktop\\exam.txt");
Scanner input = new Scanner(f);
//read from file direcly into the constructor
while (input.hasNext()) {
Question q = new Question((input.nextLine())
, (input.nextLine()),
(input.nextLine()),
(input.nextLine()),
(input.nextLine()));
//adding the question and the answers to an array
Questions.add(q);
number_of_lines++;
q.set_qIndex(number_of_lines);
Answers.add(new Answer(q));
}
input.close();
} catch (FileNotFoundException nf) {
System.out.println("couldn't open file");
}
//return number of lines in the file
return number_of_lines;
}
public void addButtonsToFrame(ArrayList<Question> q, int number_of_lines,
ArrayList<ButtonGroup> BG, ArrayList<JPanel> JP) {
int j = 0;
for (int i = 0; i < q.size(); i++) {
qNumber = i;
BG.add(new ButtonGroup());
JP.add(new JPanel());
JP.get(i).setSize(499, 400);
//creating buttons
JRadioButton option1 = new JRadioButton(q.get(i).get_option1());
option1.setActionCommand(q.get(i).get_option1());
JRadioButton option2 = new JRadioButton(q.get(i).get_option2());
option2.setActionCommand(q.get(i).get_option2());
JRadioButton option3 = new JRadioButton(q.get(i).get_option3());
option3.setActionCommand(q.get(i).get_option3());
JRadioButton option4 = new JRadioButton(q.get(i).get_option4());
option4.setActionCommand(q.get(i).get_option4());
//adding to group buttons
BG.get(j).add(option1);
BG.get(j).add(option2);
BG.get(j).add(option3);
BG.get(j).add(option4);
//adding the buttons to the panel
JP.get(j).add(option1);
JP.get(j).add(option2);
JP.get(j).add(option3);
JP.get(j).add(option4);
//setting layout that matches our goal
this.setLayout(new GridLayout(number_of_lines + 1, 1));
//set title and border for each question
JP.get(i).setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "question number " + qNumber + ": " + q.get(i).get_question()));
//adding the panel to the frame
this.add(JP.get(j));
//BG.get(i).getSelection()
JP.get(j).setVisible(true);
j++;
}
}
public void checkQuiz(ArrayList<ButtonGroup> BG, ArrayList<Answer> A) {
ArrayList<String> Selections = new ArrayList<>();
int CORRECT_ANSWER = 0;
for (int i = 0; i < BG.size(); i++) {
if (BG.get(i).getSelection().getActionCommand()
.compareTo(A.get(i).get_answer()) == CORRECT_ANSWER) {
finalscore = finalscore + 10;
}
}
}
编辑-
发布了我的工作代码。i)您需要在获取操作命令之前设置它,JRadioButton option1 = new JRadioButton(q.get(i).get_option1());
option1.setActionCommand(q.get(i).get_option1());
JRadioButton option2 = new JRadioButton(q.get(i).get_option2());
option2.setActionCommand(q.get(i).get_option2());
JRadioButton option3 = new JRadioButton(q.get(i).get_option3());
option3.setActionCommand(q.get(i).get_option3());
JRadioButton option4 = new JRadioButton(q.get(i).get_option4());
option4.setActionCommand(q.get(i).get_option4());
ii)您尚未在checkquick
方法的末尾设置标签中的最终分数,lblScore.setText(“您的最终分数:+finalscore”)代码>
iii)不建议使用while
循环。另外,您使用的当前逻辑在大多数情况下都不会调用checkquick
方法。因此,使用ActionListener
接口,而不是while
Submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
checkQuiz(BG, Answers);
}
});
我在您的代码中找不到任何EventListener
。另外,您在DisplayOnPanel
中创建的frame
对象没有任何用处,因为它在任何地方都没有使用。这里,EventListener的问题是,由于我尝试与CheckQuike方法进行比较,因此得到了nullPointerException。我如何使用它?非常感谢。我尝试使用EvenListener—发现我无法调用另一个与数组一起工作的方法—一直都会出现null异常。“在用户单击“提交”按钮之前,程序应该是“运行”的,但我相信它不会发生。”
swing不是这样工作的。您不需要循环,这将冻结您的gui。只需在提交按钮上使用ActionListener
,并在事件发生时(单击按钮或按键)在单选按钮上使用isSelected()
,检查选择了哪些单选按钮。谢谢@Ansharja,这是一个好主意-但我尝试这样做时会得到一个空异常。当我出于某种原因这样做时,BG和其他数组都是空的。谢谢!!!“setActionCommand”与“常规”ActionListener不具备的功能是什么?setActionCommand
以String
格式设置操作命令,这有助于您在ActionListener
中识别要执行的操作和ActionEvent