2015-12-18 30 views
1

我正在制作欧洲最高山脉的地图,并且想要创建一个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); 
       } 
} 
+1

究竟是什么问题?有什么错误? –

+0

我收到以下错误:方法addSelectionListener(new SelectionAdapter(){})未定义类型字符串 – HRS

回答

1

创建buttonarray一个S

for(int i = 0; i < btnArr.length; i++) { 
    btnArr[i].addSelectionListener(new SelectionAdapter()....) 
} 

此外,优化你的代码,可以考虑创建一个自定义SelectionListener中

+0

感谢看起来像一个很好的方式来做到这一点,但是当试图实现数组时,我得到以下错误:btnArr无法解析为变量。这里有什么建议? – HRS

+0

你必须声明它。检查我的编辑。 '按钮[] btnArr =新...' –

1

要实现您的第一个目标,只需使用按钮创建List,并在每次迭代中插入SelectionAdapter。

List<Button> buttons = // fill the list 

for (Button b : buttons) { 
    b.addSelectionListener(new SelectionAdapter() {....}); 
} 
0

在你的代码中,你要为你的字符串添加一个SelectionListener。 您需要将侦听器添加到Button中。此外,代码可以简化一点,比如在专用方法中使用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); 
     } 
    } 
} 

这假定所需的变量和这样是全局的。如果不是,您可以随时将它们作为参数传递,或为相关值创建一个Bean(Wrapper对象),例如Country Bean。