简体   繁体   English

在Java中使用for循环创建按钮

[英]Creating buttons with for loop in Java

I am making a map over the highest mountains in europe and want to create a for loop that makes the actions for the buttons for me. 我正在绘制欧洲最高山脉上的地图,并想要创建一个for循环,以便为我执行按钮的操作。

I am currently stuck at this code: 我目前停留在此代码:

String Text = "btn" + i;
        Text.addSelectionListener(new SelectionAdapter()

I want to add: 我要添加:

btn1.addSelectionListener(new SelectionAdapter()

btn2.addSelectionListener(new SelectionAdapter()

btn3.addSelectionListener(new SelectionAdapter()

btn4.....

when running the for loop. 在运行for循环时。 Does anyone know how to do this? 有谁知道如何做到这一点?

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

Create an array of button s: 创建一个button array

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

& loop through it: 并循环遍历:

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

Plus, to optimize your code, consider creating a custom SelectionListener . 另外,要优化代码,请考虑创建一个自定义的SelectionListener

To achieve your first goal, just create a List with the buttons, and insert SelectionAdapter in each iteration. 为了实现您的第一个目标,只需使用按钮创建一个List ,然后在每次迭代中插入SelectionAdapter。

List<Button> buttons = // fill the list

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

In your code, you are adding a SelectionListener to your String. 在您的代码中,您正在将SelectionListener添加到您的String中。 You will need to add the listener to your Button. 您将需要将侦听器添加到您的按钮。

Furthermore, the code can be streamlined a bit, such as having the SelectionListener in a dedicated method. 此外,可以对代码进行简化,例如在专用方法中具有SelectionListener。

An example: 一个例子:

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

This assumes the required variables and such is global. 这假定所需的变量,并且是全局变量。 If not, you could always just pass them across as parameters, or create a Bean (Wrapper Object) for the related values, Such as a Country Bean. 如果没有,您总是可以将它们作为参数传递,或者为相关值创建一个Bean(包装对象),例如Country Bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM