📅  最后修改于: 2020-09-30 05:58:55             🧑  作者: Mango
JList类的对象表示文本项的列表。可以设置文本项列表,以便用户可以选择一项或多项。它继承了JComponent类。
我们来看一下javax.swing.JList类的声明。
public class JList extends JComponent implements Scrollable, Accessible
Constructor | Description |
---|---|
JList() | Creates a JList with an empty, read-only, model. |
JList(ary[] listData) | Creates a JList that displays the elements in the specified array. |
JList(ListModel |
Creates a JList that displays elements from the specified, non-null, model. |
Methods | Description |
---|---|
Void addListSelectionListener(ListSelectionListener listener) | It is used to add a listener to the list, to be notified each time a change to the selection occurs. |
int getSelectedIndex() | It is used to return the smallest selected cell index. |
ListModel getModel() | It is used to return the data model that holds a list of items displayed by the JList component. |
void setListData(Object[] listData) | It is used to create a read-only ListModel from an array of objects. |
import javax.swing.*;
public class ListExample
{
ListExample(){
JFrame f= new JFrame();
DefaultListModel l1 = new DefaultListModel<>();
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
JList list = new JList<>(l1);
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ListExample();
}}
输出:
import javax.swing.*;
import java.awt.event.*;
public class ListExample
{
ListExample(){
JFrame f= new JFrame();
final JLabel label = new JLabel();
label.setSize(500,100);
JButton b=new JButton("Show");
b.setBounds(200,150,80,30);
final DefaultListModel l1 = new DefaultListModel<>();
l1.addElement("C");
l1.addElement("C++");
l1.addElement("Java");
l1.addElement("PHP");
final JList list1 = new JList<>(l1);
list1.setBounds(100,100, 75,75);
DefaultListModel l2 = new DefaultListModel<>();
l2.addElement("Turbo C++");
l2.addElement("Struts");
l2.addElement("Spring");
l2.addElement("YII");
final JList list2 = new JList<>(l2);
list2.setBounds(100,200, 75,75);
f.add(list1); f.add(list2); f.add(b); f.add(label);
f.setSize(450,450);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "";
if (list1.getSelectedIndex() != -1) {
data = "Programming language Selected: " + list1.getSelectedValue();
label.setText(data);
}
if(list2.getSelectedIndex() != -1){
data += ", FrameWork Selected: ";
for(Object frame :list2.getSelectedValues()){
data += frame + " ";
}
}
label.setText(data);
}
});
}
public static void main(String args[])
{
new ListExample();
}}
输出: