共同行动监听器按钮3 [英] Common Action Listener for 3 Buttons
问题描述
我有我的code的设计麻烦。我有3个按钮不是一个按钮小组。我想 - 根据所选择的按钮 - 执行操作。现在的操作需要在类的对象的修饰。这意味着我不能使用一个内部类,因为这并不能访问外。如果我可以添加一个事件侦听器添加到按钮组,这将是很容易,但我看到它,我将需要为每个单选按钮的事件处理程序,这是正确的?如果不是我还能怎么办呢?谢谢
一个简单的例子
公共类测试(){
单选1 =新RadoButton();
单选2 =新RadoButton();
单选3 =新RadoButton();
对象myObject的=新的对象(); 公共无效clickEvent(){
如果(1.isSelected()){
myObject.doOne();
}否则如果(2.isSelected()){
myObject.doTwo();
} .....
}
}
您可以在同一个监听器设置所有按钮。
伪code:
radioButton1 =新的单选按钮();
radioButton2 =新的单选按钮();
radioButton3 =新的单选按钮();听众=新的ActionListener(){
...
}
radioButton1.addActionListener(监听);
radioButton2.addActionListener(监听);
radioButton3.addActionListener(监听);
I am having trouble with the design of my code. I have 3 buttons not in a button group. I want to - based on the selected button - perform an action. Now the action requires a modification of an object in the class. This means i cannot use an inner class because this does not have access to the outer. If i could add an event listener to a button group this would be much easier but as i see it i will need an event handler for each radio button, is this correct? If not how else can i do it? Thanks
A quick example
public class Test(){
RadioButton 1 = new RadoButton();
RadioButton 2 = new RadoButton();
RadioButton 3 = new RadoButton();
Object myObject = new Object();
public void clickEvent(){
if(1.isSelected()){
myObject.doOne();
}else if(2.isSelected()){
myObject.doTwo();
}.....
}
}
You can set the same listener to all your buttons.
Pseudo code:
radioButton1 = new RadioButton();
radioButton2 = new RadioButton();
radioButton3 = new RadioButton();
listener = new ActionListener() {
...
}
radioButton1.addActionListener(listener);
radioButton2.addActionListener(listener);
radioButton3.addActionListener(listener);
这篇关于共同行动监听器按钮3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!