public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setSize(300,300);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
JLabel lbl=new JLabel("我的女友列表:");
panel.add(lbl);
//定义一个 finall list
final JList list=new JList();
//自定义大小
list.setPreferredSize(new Dimension(100,35));
//允许间断选择
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//Vector集合装数据
Vector<String> dataList=new Vector<String>();
dataList.add("凤姐");
dataList.add("芙蓉");
dataList.add("其坤");
dataList.add("房价");
//给下拉加数组
list.setListData(dataList);
// panel.add(list);
//将list添加到滚动面板再将滚动面板添加到panel
panel.add(new JScrollPane(list));
//设置选择项
// list.setSelectedIndex(2); //选中一个
list.setSelectedIndices(new int[]{1,2}); //选中多个
JButton button=new JButton("按钮");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int[]index=list.getSelectedIndices();
for(int i=0;i<index.length;i++){
System.out.println(index[i]);
}
}
});
panel.add(button);
jf.setContentPane(panel);
jf.setVisible(true);
}
Swing测试JLIST
最新推荐文章于 2024-01-07 09:00:00 发布