用Java创建for循环按钮

时间:2015-12-18 11:23:57

标签: java

我正在制作一张欧洲最高山脉的地图,并希望创建一个for循环,为我创建按钮的动作。

我目前仍然坚持使用此代码:

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);
                }
}

3 个答案:

答案 0 :(得分:1)

创建array button s:

Button[] btnArr = new Button[] {
    btn1, btn2, btn3,...
}

&安培;循环通过它:

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

另外,要优化代码,请考虑创建自定义 SelectionListener

答案 1 :(得分:1)

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

List<Button> buttons = // fill the list

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

答案 2 :(得分:0)

在您的代码中,您将向String添加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(包装器对象),例如Country Bean。