标签:
列表框同时可以在信息呈现给用户的列表多个选项,使用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(); } }
程序截图:
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Java Swing编程接口(30)---列表框:JList
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4658756.html