gpt4 book ai didi

java - 如何从创建的单选按钮类的实例中获取文本

转载 作者:行者123 更新时间:2023-12-01 12:38:59 25 4
gpt4 key购买 nike

这段代码可以运行,但新的问题是我可以在每个部分中选择多个单选按钮,这是不应该发生的。

此外,它计算的分数也是错误的。请有人帮我解决它吗?谢谢!!!

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class Q_Panel extends JPanel
{
//This is the radiobutton class

public JRadioButton one = new JRadioButton("none");
public JRadioButton two = new JRadioButton("none");
public JRadioButton three = new JRadioButton("none");
public JRadioButton four = new JRadioButton("none");
public String answer = new String("none");

public JLabel qLabel = new JLabel("none");

public ButtonGroup group = new ButtonGroup();

public Q_Panel()
{
group.add(one);
group.add(two);
group.add(three);
group.add(four);

QuoteListener listener = new QuoteListener();
one.addActionListener(listener);
two.addActionListener(listener);
three.addActionListener(listener);
four.addActionListener(listener);

add(qLabel);
add(one);
add(two);
add(three);
add(four);

}

private class QuoteListener implements ActionListener
{
//When I run this I do not notice it work...
public void actionPerformed (ActionEvent event)
{

String selected_answer = "";
Object source = event.getSource();

if(source == one)
answer = one.getText();
else
if(source == two)
answer = two.getText();
else
if(source == three)
answer = three.getText();
else
answer = four.getText();


System.out.println("Answer chosen : " + selected_answer);

}

}
};


class radio_button
{
//This is the the class where an instance of the radiobutton class is initialized
public static JFrame frame;
public static JPanel panel;
public static JLabel qLabel;
public static JButton Submit;
public static Q_Panel[] qnum;
public static String right_answer[];
public static int Number_of_lines;
public static String right[];



public static void create()
{
frame = new JFrame();
frame.setTitle("Sample Radio Buttons");
frame.setSize(500, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

qnum = new Q_Panel[10];
right = new String[10];
right_answer = new String[10];
submit = new JButton("Submit");

int y = 0;
int i = 0;
while( i < 10)
{

y = 60 * i;

//We want to assume that the right answer is only "male"
right_answer[i] = "male";

qLabel = new JLabel ("Name of Exam file: " + i);
qLabel.setBounds(10, y+20, 500, 25);
frame.add(qLabel);

qnum[i] = new Q_Panel();
qnum[i].one = new JRadioButton();
qnum[i].one.setText("male");
qnum[i].one.setBounds(10, y+40, 150, 25);
frame.add(qnum[i].one);

qnum[i].two = new JRadioButton();
qnum[i].two.setText("female");
qnum[i].two.setBounds(10, y+60, 150, 25);
frame.add(qnum[i].two);

qnum[i].three = new JRadioButton();
qnum[i].three.setText("shemale");
qnum[i].three.setBounds(200, y+40, 400, 25);
frame.add(qnum[i].three);

qnum[i].four = new JRadioButton();
qnum[i].four.setText("exmale");
qnum[i].four.setBounds(200, y+60, 400, 25);
frame.add(qnum[i].four);

i++;
}

submit.setBounds(400, y+80, 450, 50);
final int v = 0;
submit.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent ae)
{
//This is supposed to display all the text of the buttons selected and calculate the score in comparison to the right answer..

System.out.println("Submit has been pressed");

int right_answer_count = 0;
int score = 0;
String answer_chosen = "";
boolean checked = true;
int i = 0;

while( i < 10)
{
answer_chosen = "nil";
if (qnum[i].one.isSelected())
{
answer_chosen = qnum[i].one.getText();
}
else if (qnum[i].two.isSelected())
{
answer_chosen = qnum[i].two.getText();
}
else if (qnum[i].three.isSelected())
{
answer_chosen = qnum[i].three.getText();
}
else if (qnum[i].four.isSelected())
{
answer_chosen = qnum[i].four.getText();
}
else if (answer_chosen.equals("nil"))
{
checked = false;
}

if(answer_chosen.equals(right_answer[i]))
{
right_answer_count++;
}

System.out.println("Answer Chosen: " + answer_chosen);
System.out.println("This is the answer in qnum: "+ qnum[i].answer);

i++;
}

System.out.println("I: "+i);
System.out.println("Count right: "+right_answer_count);
score = (right_answer_count/i);
System.out.println("Score: " + score);

}
});

frame.add(submit);
frame.getContentPane().add(panel);
frame.setVisible(true);
}

public static void main(String args[])
{
create();
}
}

最佳答案

基本答案是,您过度使用了静态...

您将 Q_Panel 中的 JRadioButton 声明为...

public static JRadioButton one = new JRadioButton("none");
public static JRadioButton two = new JRadioButton("none");
public static JRadioButton three = new JRadioButton("none");
public static JRadioButton four = new JRadioButton("none");

然后在 radio_button 类中你开始做类似的事情......

qnum = new Q_Panel[10];

qnum[i].one = new JRadioButton();
qnum[i].one.setText("male");

问题是,Q_Panel 的每个实例都使用 onetwo Three 的最后一个实例和 four...这意味着如果您在 qnum[0] 中选择 one,则所有其他实例也将选择 one ...

删除static引用并修改代码以允许Q_Panel管理JRadio按钮。您甚至可以为 Q_Panel 提供一个方法,该方法将返回一个 intString 或一个 enum 来描述什么已选择...

关于java - 如何从创建的单选按钮类的实例中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299031/

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