publicclassTestextendsJFrame{privateJPanelt_1=newJPanel();privateJPanelt_0=newJPanel();privateJLabelj_1=newJLabel("题目1.");privateJLabelanwser=newJLabel("");privateButto...
public class Test extends JFrame{
private JPanel t_1=new JPanel();
private JPanel t_0=new JPanel();
private JLabel j_1=new JLabel("题目1.");
private JLabel anwser=new JLabel("");
private ButtonGroup group1 = new ButtonGroup();
private int a=0;
public Test()
{ setSize(600,600);
setTitle("单选选择题");
layoutt_1();
layoutt_0();
setLayout(new GridLayout(4,1));
add(j_1);add(t_1);
add(t_0);add(anwser);
}
private void layoutt_1()
{JRadioButton A = new JRadioButton("A.");
JRadioButton B = new JRadioButton("B.");
JRadioButton C = new JRadioButton("C.");
JRadioButton D = new JRadioButton("D.");
A.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
a++;
}
});
t_1.setLayout(new GridLayout(2,2));
t_1.add(A);group1.add(A);
t_1.add(B);group1.add(B);
t_1.add(C);group1.add(C);
t_1.add(D);group1.add(D);
}
private void layoutt_0()
{
JButton button_2=new JButton("提交答案");
button_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
anwser.setText("正确的答题数为:"+a+"题");
}
}
t_0.add(button_1);
}
public static void main(String[] args)
{
Test test_1=new Test();
test_1.setVisible(true);
}
}
源代码太长被我删了很多,程序就是要求5个单选题,两个按钮:提交和重新输入,提交后显示答对题数。因为每个正确选项都有事件监听,所以运行后若先选正确选项在改为错误选项后,其显示的答对数量会比真实的多1,可是我又不知道怎么改。。。
展开