我正在製作歐洲最高山脈的地圖,並且想要創建一個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