package swing; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Vector; public class sandemo2 extends JFrame{ JList list1,list2; Container container; public sandemo2() { container=this.getContentPane(); this.setLayout(new GridLayout(1,2)); String []str= {"北京","上海","杭州","深圳"}; Vector <String>vector=new Vector<String>(); vector.add("篮球"); vector.add("乒乓球"); vector.add("跑步"); list2=new JList(vector); list1=new JList(str); list1.setBorder(BorderFactory.createTitledBorder("你喜欢的城市")); list2.setBorder(BorderFactory.createTitledBorder("你喜欢的运动是")); list1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//设置标题 container.add(list1); container.add(list2); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); this.setTitle("列表框练习二"); this.setSize(600,450); this.show(); } public static void main(String []args) { new sandemo2(); } }
运行结果: