此代码运行但新问题是我能够在每个部分中选择多个不应该发生的单选按钮。
此外,它计算得分错误。有人可以帮我解决吗?谢谢!!!
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();
}
}
答案 0 :(得分:1)
基本答案是,您过度使用static
...
您将JRadioButton
中的Q_Panel
声明为...
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
的每个实例都在使用one
,two
,three
和four
的最后一个实例...这意味着您在one
中选择qnum[0]
,所有其他实例也将选择one
...
删除static
引用并修改代码,以允许Q_Panel
管理JRadio
按钮。您甚至可以为Q_Panel
提供一种方法,该方法会返回int
或String
或enum
,其中描述了所选内容......