向 java 中的多个 'choice' 对象添加选项

Adding options to multiple 'choice' objects in java

for 循环不工作。我得到 "Choice cannot be resolved to a variable"。我想向添加到泛型列表的多个 Choice 对象添加一组选项。在此示例中,您可以看到这些对象之一 (DidWell1)。

import java.awt.Choice;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;


public class testing {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    testing window = new testing();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public testing() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Choice DidWell1 = new Choice();
        DidWell1.setBounds(119, 36, 135, 20);
        frame.getContentPane().add(DidWell1);

        List<Choice> list = new ArrayList<Choice>();

        list.add(DidWell1);

        String option = "Friday";

        For(Choice choice: list){
            choice.add( option );
        }
    }

}

创建 Choice 对象列表

List<Choice> list = new ArrayList<Choice>();

接下来将所有 Didwells 添加到此列表中。

list.add( didwell1 ); etc

通过此设置,可以使用 For 循环添加新选项:

String option = "Friday";

for( Choice choice: list ){
    choice.add( option );
}

或者使用 While 循环:

Iterator<Choice> iter = new list.iterator();
while iterator.hasNext(){
    iter.next().add( option );
}