最基本的单选按钮使用,见笑了。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Check extends JFrame implements ActionListener
{JRadioButton c1,c2;ButtonGroup b;
public static void main(String args[])
{new Check();
}
public Check()
{super("单选按钮");
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
b=new ButtonGroup();
c1=new JRadioButton("启用",true);
c2=new JRadioButton("禁用",false);
b.add(c1);b.add(c2);
c1.setEnabled(false);
setLayout(null);add(c1);add(c2);
c1.setBounds(60,140,60,20);c2.setBounds(180,140,60,20);
c1.addActionListener(this);
c2.addActionListener(this);
setVisible(true);
setSize(300,240);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==c2)
{setTitle("启用第二个单选按钮");
c1.setEnabled(true);c2.setEnabled(false);
}
else
{setTitle("启用第一个单选的按钮");
c1.setEnabled(false);c2.setEnabled(true);
}
}
}
评论