• Java——列表框:JList


    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.JRadioButton;
    import javax.swing.ListSelectionModel;
    
    //=================================================
    // File Name       :	JList_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    //类名:MyList
    //属性:
    //方法:
    class MyList{
    	private JFrame frame = new JFrame("窗体");					//定义窗体
    	private Container cont = frame.getContentPane();			//得到窗体容器
    	private JList list1  = null;															//定义列表框
    	private JList list2  = null;															//定义列表框
    	public MyList(){
    		this.frame.setLayout(new GridLayout(1,3));					//定义排版,1行2列
    		String nations[] = {"中国","美国","韩国","法国","英国"};
    		Vector<String> v = new Vector<String>();						//定义一个Vector集合
    		v.add("Vector1");
    		v.add("Vector2");
    		v.add("Vector3");
    		this.list1 = new JList(nations);					//实例化JList
    		this.list2 = new JList(v);								//实例化JList
    		//定义一个列表框的边框显示条
    		list1.setBorder(BorderFactory.createTitledBorder("哪个国家?"));
    		list2.setBorder(BorderFactory.createTitledBorder("Vector?"));
    		//第一个列表框一次可以选择多个选项
    		list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    		//第二个列表框每次可以选择一个选项
    		list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		cont.add(this.list1);					//加入面板
    		cont.add(this.list2);					//加入面板
    		this.frame.setSize(330,200);
    		this.frame.setVisible(true);
    		this.frame.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
    			// TODO 自动生成的方法存根
    			System.out.println("windowClosing-->窗口关闭");
    			System.exit(1);
    			}
    		});
    	}
    }
    
    
    //主类
    //Function        : 	JList_demo
    public class JList_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new MyList();
    	}
    
    }
    

    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.AbstractListModel;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.ListSelectionModel;
    
    //=================================================
    // File Name       :	JModel_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    //类名:MyListModel
    //属性:
    //方法:
    class MyListModel extends AbstractListModel{
    
    	private String nations[] = {"中国","美国","韩国","法国","英国"};
    	
    	@Override
    	public int getSize() {							
    		// TODO 自动生成的方法存根
    		return this.nations.length;
    	}
    
    	@Override
    	public Object getElementAt(int index) {		//返回指定标号的元素
    		// TODO 自动生成的方法存根
    		if(index<this.nations.length){
    			return this.nations[index];
    		}else{
    			return null;
    		}
    	}
    }
    
    //类名:MyList
    //属性:
    //方法:
    class myList{
    	private JFrame frame = new JFrame("窗体");					//定义窗体
    	private Container cont = frame.getContentPane();			//得到窗体容器
    	private JList list1  = null;															//定义列表框
    	private JList list2  = null;															//定义列表框
    	public myList(){
    		this.list1 = new JList(new MyListModel());
    		this.list2 = new JList(new MyListModel());
    		//定义一个列表框的边框显示条
    		list1.setBorder(BorderFactory.createTitledBorder("哪个国家?"));
    		list2.setBorder(BorderFactory.createTitledBorder("Vector?"));
    		//第一个列表框一次可以选择多个选项
    		list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    		//第二个列表框每次可以选择一个选项
    		list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		cont.add(this.list1);					//加入面板
    		cont.add(this.list2);					//加入面板
    		this.frame.setSize(330,200);
    		this.frame.setVisible(true);
    		this.frame.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
    			// TODO 自动生成的方法存根
    			System.out.println("windowClosing-->窗口关闭");
    			System.exit(1);
    			}
    		});
    	}
    }
    
    //主类
    //Function        : 	JModel_demo
    public class JModel_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new MyList();
    	}
    
    }
    

    监听只能加一个

    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.AbstractListModel;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    //=================================================
    // File Name       :	JModel_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    //类名:MyListModel
    //属性:
    //方法:
    class MyListModel extends AbstractListModel{
    
    	private String nations[] = {"中国","美国","韩国","法国","英国"};
    	
    	@Override
    	public int getSize() {							
    		// TODO 自动生成的方法存根
    		return this.nations.length;
    	}
    
    	@Override
    	public Object getElementAt(int index) {		//返回指定标号的元素
    		// TODO 自动生成的方法存根
    		if(index<this.nations.length){
    			return this.nations[index];
    		}else{
    			return null;
    		}
    	}
    }
    
    //类名:MyList
    //属性:
    //方法:
    class myList implements ListSelectionListener{
    	private JFrame frame = new JFrame("窗体");					//定义窗体
    	private Container cont = frame.getContentPane();			//得到窗体容器
    	private JList list1  = null;															//定义列表框
    //	private JList list2  = null;															//定义列表框
    	public myList(){
    		this.list1 = new JList(new MyListModel());
    //		this.list2 = new JList(new MyListModel());
    		//定义一个列表框的边框显示条
    		list1.setBorder(BorderFactory.createTitledBorder("哪个国家?"));
    //		list2.setBorder(BorderFactory.createTitledBorder("Vector?"));
    		//第一个列表框一次可以选择多个选项
    		list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    		//第二个列表框每次可以选择一个选项
    //		list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		
    		list1.addListSelectionListener(this); 			//加入事件监听
    //		list2.addListSelectionListener(this); 			//加入事件监听
    		cont.add(new JScrollPane(this.list1));			//加入滚动条
    		
    		cont.add(this.list1);					//加入面板
    //		cont.add(this.list2);					//加入面板
    		this.frame.setSize(330,200);
    		this.frame.setVisible(true);
    		this.frame.addWindowListener(new WindowAdapter(){	//加入事件监听
    			public void windowClosing(WindowEvent arg0) {			//窗口关闭时触发,按下关闭按钮
    			// TODO 自动生成的方法存根
    			System.out.println("windowClosing-->窗口关闭");
    			System.exit(1);
    			}
    		});
    	}
    	
    	public void valueChanged(ListSelectionEvent e) {
    		// TODO 自动生成的方法存根
    		int temp[] = list1.getSelectedIndices();				//取得全部选定
    		System.out.println("选定的内容:");
    		for(int i=0;i<temp.length;i++){
    			System.out.println(list1.getModel().getElementAt(i)+"、");
    		}
    	}
    }
    
    //主类
    //Function        : 	JModel_demo
    public class JModel_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new myList();
    	}
    
    }
    
  • 相关阅读:
    前台开发——处理用户密码登录的修改功能
    前台开发——处理用户收货信息的修改
    Max Sum -- hdu -- 1003
    (深搜)Oil Deposits -- hdu -- 1241
    (博弈 sg入门2)
    (博弈 sg入门)kiki's game -- hdu -- 2147
    (巴什博弈 sg函数入门1) Brave Game -- hdu -- 1846
    (全排列)Ignatius and the Princess II -- HDU -- 1027
    (模拟)Arithmetic Sequence -- HDU -- 5400
    (字符串 键盘转换)Convert QWERTY to Dvorak -- zoj -- 5526
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5316538.html
Copyright © 2020-2023  润新知