gpt4 book ai didi

java - Eclipse SWT 小部件 : Combining two Listeners

转载 作者:行者123 更新时间:2023-11-29 04:25:35 32 4
gpt4 key购买 nike

我目前正在更新向导页面,我希望在选择 radioButton 后禁用 textField

例如:if (radioButton[1].getSelection)
“那么让我们说 textField[2].setEnabled(false)”

这里的问题是,如何将 radioButton[i]textField[i] 的监听器连接在一起?谢谢。

 case composed:
container.setLayout(new RowLayout());
Button[] radioButton= new Button[answers.size()];
for (int i = 0; i < answers.size(); i++) {
String ans = answers.get(i).getValue();
radioButton[i]=new Button(container, SWT.RADIO);
radioButton[i].setText(ans);
radioButton[i].addSelectionListener(new SelectionAdapter(){

@Override
public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub

}});
}
Text[] textField=new Text[answers.size()];
for (int i = 0; i < answers.size(); i++) {
textField[i]=new Text(container, SWT.SINGLE | SWT.BORDER);
textField[i].addModifyListener(new ModifyListener(){

@Override
public void modifyText(ModifyEvent arg0) {
// TODO Auto-generated method stub

}

});
}

最佳答案

在添加监听器之前创建单选按钮和文本字段,以便您可以同时引用它们。

        Button[] radioButton = new Button[answers.size()];
for (int i = 0; i < answers.size(); i++) {
String ans = answers.get(i).getValue();
radioButton[i] = new Button(container, SWT.RADIO);
radioButton[i].setText(ans);
}

Text[] textField = new Text[answers.size()];
for (int i = 0; i < answers.size(); i++) {
textField[i] = new Text(container, SWT.SINGLE | SWT.BORDER);

radioButton[i].addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent arg0) {
textField[i].setEnabled(radioButton[i].getSelection());
}});

textField[i].addModifyListener(new ModifyListener(){
@Override
public void modifyText(ModifyEvent arg0) {
// your stuff
}});
}

您也可以将两个监听器设为同一个监听器对象(但老实说,为什么要这么麻烦 - 最好是具有单一用途的较小监听器):

private static class MyListener extends SelectionAdapter implements ModifyListener {
public void widgetSelected(SelectionEvent arg) { ... }
public void modifyText(ModifyEvent arg) { ... }
}

关于java - Eclipse SWT 小部件 : Combining two Listeners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46530175/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com