import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Answer6_2 extends Frame implements ActionListener{
public Answer6_2(){
CheckboxGroup cg = new CheckboxGroup();
Checkbox c1 = new Checkbox("启用",cg,false);
Checkbox c2 = new Checkbox("禁用",cg,false);
this.setLayout(new GridLayout(1,2));
c1.addActionListener(this);
c2.addActionListener(this);
this.add(c1);this.add(c2);
this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
this.setSize(350,200);
this.setVisible(true);
}
public static void main(String args[]){
Answer6_2 f = new Answer6_2();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==c1) f.setTitle("启用第一个单选按钮");
else f.setTitle("启用第二个单选按钮");
}
}里面凡是含有c1 c2的语句都说找不到符号PS:如果用JFrame呢?JCheckbox,好像单选按钮没有JChecboxGroup,得用什么JRadioButton
求解答
import java.awt.*;
import java.awt.event.*;
public class Answer6_2 extends Frame implements ActionListener{
public Answer6_2(){
CheckboxGroup cg = new CheckboxGroup();
Checkbox c1 = new Checkbox("启用",cg,false);
Checkbox c2 = new Checkbox("禁用",cg,false);
this.setLayout(new GridLayout(1,2));
c1.addActionListener(this);
c2.addActionListener(this);
this.add(c1);this.add(c2);
this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
this.setSize(350,200);
this.setVisible(true);
}
public static void main(String args[]){
Answer6_2 f = new Answer6_2();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==c1) f.setTitle("启用第一个单选按钮");
else f.setTitle("启用第二个单选按钮");
}
}里面凡是含有c1 c2的语句都说找不到符号PS:如果用JFrame呢?JCheckbox,好像单选按钮没有JChecboxGroup,得用什么JRadioButton
求解答
radioButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(radioButton1.isSelected()){
radioButton.setSelected(false);//
radioButton1.setSelected(true);
}
}
});
JRadioButton radioButton = new JRadioButton("启用");
radioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(radioButton.isSelected()){
radioButton.setSelected(true);//
radioButton1.setSelected(false);
}
}
});
JRadioButton b1 = new JRadioButton("");
ButtonGroup bg = new ButtonGroup();
bg.add(b1); 放在一个ButtonGroup里面, 实现单选.
2楼:JCheckBox是不是和JRadioButton等价?
ActionListener中的f.set.Title()都改成this.Title()
JCheckBox是不是和JRadioButton等价?
我用的是getSource,获取对象源