我正在制作欧洲最高山脉的地图,并且想要创建一个for循环,为我制作按钮的动作。在Java中使用for循环创建按钮
我目前停留在这个代码:
String Text = "btn" + i;
Text.addSelectionListener(new SelectionAdapter()
我想补充:
btn1.addSelectionListener(new SelectionAdapter()
btn2.addSelectionListener(new SelectionAdapter()
btn3.addSelectionListener(new SelectionAdapter()
btn4.....
for循环运行时。 有谁知道如何做到这一点?通过它
Button[] btnArr = new Button[] {
btn1, btn2, btn3,...
}
&环:
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);
}
}
究竟是什么问题?有什么错误? –
我收到以下错误:方法addSelectionListener(new SelectionAdapter(){})未定义类型字符串 – HRS