请问各位高手,我想问一个问题,就是读出一个包含试题的文本文件,怎样在每个题目的选项前添加JRadio这个组件?
以前写的
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyRadioButtons extends JFrame implements ActionListener
{
JRadioButton rad1,rad2;
ButtonGroup rbgrp=new ButtonGroup();
public MyRadioButtons()
{
setTitle("单选按钮");
JPanel cpane=(JPanel)getContentPane();
cpane.setLayout(new GridLayout());
rad1=new JRadioButton("启用");
rad1.addActionListener(this);
rad1.setActionCommand("First Activated");
rad1.setSelected(true);
cpane.add(rad1);
rad2=new JRadioButton("禁用");
rad2.addActionListener(this);
rad2.setActionCommand("Second Activated");
rad2.setSelected(true);
cpane.add(rad2);
rbgrp.add(rad1);
rbgrp.add(rad2);
radadapter radapp=new radadapter();
addWindowListener(radapp);
}
class radadapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==rad1)
{
setTitle("启用第一个单选按钮");
rad1.setEnabled(false);
rad2.setEnabled(true);
}
else if(e.getSource()==rad2)
{
setTitle("启用第二个单选按钮");
rad1.setEnabled(true);
rad2.setEnabled(false);
}
}
public static void main(String args[])
{
MyRadioButtons radb=new MyRadioButtons();
radb.setSize(300,200);
radb.setVisible(true);
}
}