列表框同时可以在信息呈现给用户的列表多个选项,使用JList能够建立一个列表框。
package com.beyole.util; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.ListSelectionModel; class MyList { private JFrame frame = new JFrame("Beyole"); private Container container = frame.getContentPane(); private JList list1 = null;// 定义列表框 private JList list2 = null;// 定义列表框 public MyList() { this.frame.setLayout(new GridLayout(1, 2)); String nation[] = { "中国", "日本", "俄罗斯", "朝鲜", "美国" }; Vector<String> vector = new Vector<String>(); vector.add("主站"); vector.add("博客"); vector.add("论坛"); this.list1 = new JList(nation); this.list2 = new JList(vector); list1.setBorder(BorderFactory.createTitledBorder("你喜欢哪个国家")); list2.setBorder(BorderFactory.createTitledBorder("你喜欢哪个站点")); list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); container.add(this.list1); container.add(this.list2); this.frame.setSize(330, 180); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(1); } }); } } public class JListDemo01 { public static void main(String[] args) { new MyList(); } }
以上程序中第一个JList通过字符串数组设置列表的内容,并通过setSelectionMode()方法设置一次性能够选择的一个选项,第二个JList通过Vector设置列表的内容。并通过setSelectionMode()方法设置一次性能够选择的多个选项
程序截图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVlamlhd2VpMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
版权声明:本文博客原创文章,博客,未经同意,不得转载。
时间: 2024-10-11 19:46:00