gpt4 book ai didi

java - 如何动态创建 SWT 按钮及其操作?

转载 作者:行者123 更新时间:2023-11-30 07:18:35 25 4
gpt4 key购买 nike

我创建了一个确定按钮(按钮)。

我想根据用户输入动态创建 1 到 10 个 SWT 按钮(复选框)。

如何创建?

如果点击确定按钮,如何显示哪些复选框按钮全部被选中?

请在下面找到我正在尝试的片段:

Set<String> Groups = getData(Contents);
for(String group : contentGroups) {

contentButton = new Button(fComposite, SWT.CHECK);

// is this right way to create dynamic buttons?

contentButton.setText(group);

}

okButton = new Button(lowComposite, SWT.PUSH);

okButton.addSelectionListener(new SelectionListener(){

@Override
public void widgetSelected(SelectionEvent e){

//Here how to get the selection status of contentButtons?
}
}

最佳答案

这将打印出按钮的选择状态:

Set<String> Groups = getData(Contents);

final List<Button> buttons = new ArrayList<Button>();

for(String group : contentGroups)
{
Button newButton = new Button(fComposite, SWT.CHECK);
newButton.setText(group);

// save the button
buttons.add(newButton);
}

Button okButton = new Button(lowComposite, SWT.PUSH);

okButton.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event e)
{
// iterate over saved buttons
for(Button button : buttons)
{
System.out.println(button.getText() + ": " + button.getSelection());
}
}
}

关于java - 如何动态创建 SWT 按钮及其操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15244030/

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