代码一:
import javax.swing.JFrame ;
import javax.swing.JList ;
import java.util.Vector ;
import javax.swing.BorderFactory ;
import javax.swing.ListSelectionModel ;
import javax.swing.JScrollPane ;
import java.awt.Container ;
import java.awt.GridLayout ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
class MyList{
private JFrame frame = new JFrame("Welcome To MLDN") ;
private Container cont = frame.getContentPane() ; // 得到窗体容器
private JList list1 = null ;
private JList list2 = null ;
public MyList(){
this.frame.setLayout(new GridLayout(1,3)) ; // 定义排版
String nations[] = {"中国","巴西","美国","韩国","意大利","法国"} ;
Vector<String> v = new Vector<String>() ;
v.add("MLDN") ;
v.add("MLDNJAVA") ;
v.add("智囊团") ;
this.list1 = new JList(nations) ;
this.list2 = new JList(v) ;
this.list1.setBorder(BorderFactory.createTitledBorder("你喜欢去那个国家旅游?")) ;
this.list2.setBorder(BorderFactory.createTitledBorder("你喜欢网站?")) ;
this.list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) ;// 多选
this.list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION) ;// 单选
this.cont.add(new JScrollPane(this.list1)) ;
this.cont.add(this.list2) ;
this.frame.setSize(330,180) ;
this.frame.setVisible(true) ;
this.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent a){
System.exit(1) ;
}
}) ;
}
}
public class JListDemo01{
public static void main(String args[]){
new MyList() ;
}
}
代码二:
import javax.swing.JFrame ;
import javax.swing.JList ;
import java.util.Vector ;
import javax.swing.BorderFactory ;
import javax.swing.ListSelectionModel ;
import javax.swing.JScrollPane ;
import javax.swing.AbstractListModel ;
import java.awt.Container ;
import java.awt.GridLayout ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
class MyListModel extends AbstractListModel{
private String nations[] = {"中国","巴西","美国","韩国","意大利","法国"} ;
public Object getElementAt(int index){
if(index<this.nations.length){
return this.nations[index] ;
}else{
return null ;
}
}
public int getSize(){
return this.nations.length ;
}
}
class MyList{
private JFrame frame = new JFrame("Welcome To MLDN") ;
private Container cont = frame.getContentPane() ; // 得到窗体容器
private JList list1 = null ;
public MyList(){
this.frame.setLayout(new GridLayout(1,3)) ; // 定义排版
String nations[] = {"中国","巴西","美国","韩国","意大利","法国"} ;
Vector<String> v = new Vector<String>() ;
v.add("MLDN") ;
v.add("MLDNJAVA") ;
v.add("智囊团") ;
this.list1 = new JList(new MyListModel()) ;
this.list1.setBorder(BorderFactory.createTitledBorder("你喜欢去那个国家旅游?")) ;
this.list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) ;// 多选
this.cont.add(new JScrollPane(this.list1)) ;
this.frame.setSize(330,180) ;
this.frame.setVisible(true) ;
this.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent a){
System.exit(1) ;
}
}) ;
}
}
public class JListDemo02{
public static void main(String args[]){
new MyList() ;
}
}
代码三:
import javax.swing.JFrame ;
import javax.swing.JList ;
import java.util.Vector ;
import javax.swing.BorderFactory ;
import javax.swing.ListSelectionModel ;
import javax.swing.JScrollPane ;
import javax.swing.AbstractListModel ;
import java.awt.Container ;
import java.awt.GridLayout ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import javax.swing.event.ListSelectionListener ;
import javax.swing.event.ListSelectionEvent ;
class MyListModel extends AbstractListModel{
private String nations[] = {"中国","巴西","美国","韩国","意大利","法国"} ;
public Object getElementAt(int index){
if(index<this.nations.length){
return this.nations[index] ;
}else{
return null ;
}
}
public int getSize(){
return this.nations.length ;
}
}
class MyList implements ListSelectionListener{
private JFrame frame = new JFrame("Welcome To MLDN") ;
private Container cont = frame.getContentPane() ; // 得到窗体容器
private JList list1 = null ;
public MyList(){
this.frame.setLayout(new GridLayout(1,3)) ; // 定义排版
String nations[] = {"中国","巴西","美国","韩国","意大利","法国"} ;
Vector<String> v = new Vector<String>() ;
v.add("MLDN") ;
v.add("MLDNJAVA") ;
v.add("智囊团") ;
this.list1 = new JList(new MyListModel()) ;
this.list1.setBorder(BorderFactory.createTitledBorder("你喜欢去那个国家旅游?")) ;
this.list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) ;// 多选
this.cont.add(new JScrollPane(this.list1)) ;
this.frame.setSize(330,180) ;
this.frame.setVisible(true) ;
this.list1.addListSelectionListener(this) ;
this.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent a){
System.exit(1) ;
}
}) ;
}
public void valueChanged(ListSelectionEvent e){ // 事件处理
int temp[] = list1.getSelectedIndices() ;
System.out.print("选定的内容:") ;
for(int i=0;i<temp.length;i++){
System.out.print(list1.getModel().getElementAt(i) + "、") ;
}
System.out.println() ;
}
}
public class JListDemo03{
public static void main(String args[]){
new MyList() ;
}
}