在Java中使用for循环创建按钮
我正在制作一张欧洲最高山脉的地图,希望创建一个for循环,为我的按钮制作动作 我目前被困在以下代码中:在Java中使用for循环创建按钮,java,Java,我正在制作一张欧洲最高山脉的地图,希望创建一个for循环,为我的按钮制作动作 我目前被困在以下代码中: String Text = "btn" + i; Text.addSelectionListener(new SelectionAdapter() 我想补充: btn1.addSelectionListener(new SelectionAdapter() btn2.addSelectionListener(new SelectionAdapter() btn3.addS
String Text = "btn" + i;
Text.addSelectionListener(new SelectionAdapter()
我想补充:
btn1.addSelectionListener(new SelectionAdapter()
btn2.addSelectionListener(new SelectionAdapter()
btn3.addSelectionListener(new SelectionAdapter()
btn4.....
当运行for循环时。
有人知道怎么做吗
for(final int i=1; i< country.length; i++){
String Text = "btn" + i;
Text.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
txtCountry= new Text(shell, SWT.BORDER);
txtCountry.setText("Country: " + country[i]);
txtCountry.setBounds(22, 112, 204, 30);
formToolkit.adapt(txtCountry, true, true);
txtHighestMountain = new Text(shell, SWT.BORDER);
txtHighestMountain.setText("Highest Mountain: " + highestPoint[i]);
txtHighestMountain.setBounds(22, 148, 204, 30);
formToolkit.adapt(txtHighestMountain, true, true);
txtMeters = new Text(shell, SWT.BORDER);
txtMeters.setText("Meters above sea level: " + MAMSL[i]);
txtMeters.setBounds(22, 184, 204, 30);
formToolkit.adapt(txtMeters, true, true);
}
}
for(最终整数i=1;i
创建按钮的数组
:
Button[] btnArr = new Button[] {
btn1, btn2, btn3,...
}
&循环通过它:
for(int i = 0; i < btnArr.length; i++) {
btnArr[i].addSelectionListener(new SelectionAdapter()....)
}
for(int i=0;i
<> Plus,为了优化代码,考虑创建自定义<强> SelectionListener < /强> ./P> < P>以实现您的第一个目标,只需使用按钮创建<代码>列表>代码>,并在每次迭代中插入SelectionAdapter。
List<Button> buttons = // fill the list
for (Button b : buttons) {
b.addSelectionListener(new SelectionAdapter() {....});
}
List按钮=//填写列表
用于(按钮b:按钮){
b、 addSelectionListener(新建SelectionAdapter(){….});
}
在代码中,您正在向字符串添加SelectionListener。
您需要将侦听器添加到按钮中
此外,代码可以稍微简化,例如在专用方法中使用SelectionListener
例如:
private Button[] getButtons() {
Button[] buttons = new Button[country.length];
for (final int i = 1; i < country.length; i++) {
String text = "btn" + i;
Button button = new Button(text);
button.addSelectionListener(getCountrySelectionListener(country[i]));
buttons[i] = button;
}
return buttons;
}
private SelectionListener getCountrySelectionListener(final String country) {
return new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
txtCountry = new Text(shell, SWT.BORDER);
txtCountry.setText("Country: " + country);
txtCountry.setBounds(22, 112, 204, 30);
formToolkit.adapt(txtCountry, true, true);
txtHighestMountain = new Text(shell, SWT.BORDER);
txtHighestMountain.setText("Highest Mountain: " + highestPoint[i]);
txtHighestMountain.setBounds(22, 148, 204, 30);
formToolkit.adapt(txtHighestMountain, true, true);
txtMeters = new Text(shell, SWT.BORDER);
txtMeters.setText("Meters above sea level: " + MAMSL[i]);
txtMeters.setBounds(22, 184, 204, 30);
formToolkit.adapt(txtMeters, true, true);
}
}
}
private按钮[]getButtons(){
按钮[]按钮=新按钮[国家/地区长度];
对于(最终整数i=1;i
这假设所需的变量是全局变量。如果不是,您可以始终将它们作为参数传递,或者为相关值创建一个Bean(包装器对象),例如Country Bean。到底是什么问题?有错误吗?我收到以下错误:方法addSelectionListener(new SelectionAdapter(){})对于类型StringThank未定义,这看起来是一个很好的方法,但是当尝试实现数组时,我得到以下错误:btnArr无法解析为变量。这里有任何建议吗?您必须声明它。请检查我的编辑。Button[]btnArr=new…