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() ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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() ; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步