标签:
JList的使用
--构造一个简单的list
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.JScrollPane; public class JList01 { public static void main(String[] args) { JFrame f=new JFrame(); Container contentpane=f.getContentPane(); f.setLayout(new GridLayout(1, 2)); String[] s=new String[]{"日本","英国","法国","中国","美国"}; Vector v=new Vector(); v.addElement("nokia 8850"); v.addElement("nokia 8250"); v.addElement("notorola v8088"); v.addElement("motorola v3688"); v.addElement("panasonic GD92"); v.addElement("其他"); JList jList=new JList(s); jList.setBorder(BorderFactory.createTitledBorder("您最喜欢到哪个国家玩呢")); JList jList2=new JList(v); jList2.setBorder(BorderFactory.createTitledBorder("你最喜欢哪部手机呢")); contentpane.add(new JScrollPane(jList)); contentpane.add(new JScrollPane(jList2)); contentpane.add(jList2); f.pack(); f.show(); f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); } }); } }
在这个窗口中,当窗口变小时,list没有滚动的效果,标红的代码段可以展示这个效果
*选择模式(Selection Mode)
设置选择模式可以用JList所提供的setSelectionMode方法(int selectionMode)
标签:
原文地址:http://www.cnblogs.com/bornfreeonekiss/p/5564126.html