Form form = new Form("List Example"); //Create Form
Button button = new Button("PRESS ME"); //Create Button
form.add(button); // add button to Form
List myList = new List<>(); //Create List
form.add(myList); //add List to Form
// Create an Array of Elements
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 20; i++)
{
arrayList.add("Elemnt " + i);
}
// Create ListModel
DefaultListModel<String> listModel = new DefaultListModel<>(arrayList);
// Add Button ActionListner
button.addActionListener(new ActionListener<ActionEvent>()
{
public void actionPerformed(ActionEvent arg0)
{
myList.setModel(listModel); //add ListModel to List
form.repaint();
}
});
form.show();
1 回答
这是一个简短的例子: