请大侠为我解一下迷惑:我想捕获选择项改变的事件,如何处理?我用了这样的方法,但是结果出乎意料:
group.getSelection().addChangeListener(new ChangeListener(){...});
请问这种用法对马?为什么?ButtonGroup 中的方法:boolean isSelected(ButtonModel m)
ButtonModel getSelection()
void setSelected(ButtonModel m, boolean b)
都是干什么用的啊,什么时候用?怎么用法?多谢了。看了jdk文档,google 了也没找到合适的答案,实在是搞不懂了!
group.getSelection().addChangeListener(new ChangeListener(){...});
请问这种用法对马?为什么?ButtonGroup 中的方法:boolean isSelected(ButtonModel m)
ButtonModel getSelection()
void setSelected(ButtonModel m, boolean b)
都是干什么用的啊,什么时候用?怎么用法?多谢了。看了jdk文档,google 了也没找到合适的答案,实在是搞不懂了!
解决方案 »
- 就剩下10可用分了,LinkedList 问题 如果嫌分少 我再申请个ID 把分都给你
- 求教两个简单的问题!
- 关于JBuilder的问题,谢谢 !!!
- 我用ant编译src目录下所有的java文件,(大约有1500个),运行后报java.lang.OutOfMemoryError,这是为什么?
- 如何判断一个变量值是空值?
- 在JAVA中怎么样执行一个存储过程,详细如下:
- 一个java的小程序
- 如何在java.text.包中用NumberFormat格式化如double,float等数据的输出呢?
- 我该向哪个方向发展?
- 这套题我做了一天都做不出来,求帮忙,JAVA学了一个月了
- 请问如何能使所画的文字居右对齐?
- 在线等待:求助,为什么在我本地(TOMCAT)上面没有问题,但部署在WebSphere却报这种错误啊???
你只需给两个button添加事件监听就可以了
没用过给buttongroup添加事件监听
JRadioButton radioButtonFirst = new JRadioButton();
JRadioButton radioButtonSecond = new JRadioButton();
JRadioButton radioButtonThird = new JRadioButton();buttonGroup.add(radioButtonFirst);
buttonGroup.add(radioButtonSecond);
buttonGroup.add(radioButtonThird);radioButtonFirst.addActionListener(...);
radioButtonSecond.addActionListener(...);
radioButtonThird.addActionListener(...);
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;public class MyBG implements ActionListener {
public static void main(String args[]) {
MyBG b=new MyBG();
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("JRadioButton1");
JRadioButton b2 = new JRadioButton("JRadioButton2");
b1.addActionListener(b);
b2.addActionListener(b);
bg.add(b1);
bg.add(b2);
JFrame f = new JFrame("ButtonGroup");
f.add(b1, "South");
f.add(b2, "North");
b1.setSelected(true);
f.setSize(400, 400);
f.setVisible(true);
Enumeration en = bg.getElements();
// System.out.println(bg.getSelection());
while (en.hasMoreElements()) {
AbstractButton ab = (AbstractButton)en.nextElement();
// System.out.println(ab);
if (ab.isSelected()) {
System.out.println(ab);
}
} } public void stateChanged(ChangeEvent e) {
System.out.println("e.getSource()"+e.getSource());
// e.getSource();
} public void actionPerformed(ActionEvent e) {
System.out.println("e.getSource()"+e.getSource());
}
}
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.AbstractButton;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;public class MyBG implements ActionListener,ChangeListener {
public static void main(String args[]) {
MyBG b=new MyBG();
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("JRadioButton1");
JRadioButton b2 = new JRadioButton("JRadioButton2");
b1.addActionListener(b);
b2.addActionListener(b);
b1.addChangeListener(b);
b2.addChangeListener(b);
bg.add(b1);
bg.add(b2);
JFrame f = new JFrame("ButtonGroup");
f.add(b1, "South");
f.add(b2, "North");
b1.setSelected(true);
f.setSize(400, 400);
f.setVisible(true);
} public void stateChanged(ChangeEvent e) {
System.out.println("Change:"+((JRadioButton)e.getSource()).getText());
e.getSource();
} public void actionPerformed(ActionEvent e) {
System.out.println("Action:"+((JRadioButton)e.getSource()).getText());
}
}