zoukankan      html  css  js  c++  java
  • 列表框JList 及动作监听

    源代码:

    <span style="font-size:18px;">import java.util.Vector ;
    import java.awt.GridLayout ;
    import java.awt.Container ;
    import java.awt.event.WindowAdapter ;
    import java.awt.event.WindowListener ;
    import java.awt.event.WindowEvent ;
    import javax.swing.JFrame ;
    import javax.swing.JList ;
    import javax.swing.JScrollPane ;
    import javax.swing.BorderFactory ;
    import javax.swing.ListSelectionModel ;
    
    class MyList
    {
        JFrame frame = new JFrame() ;
        Container con = frame.getContentPane() ;  //得到窗体容器
        private JList list1 = null ;
        private JList list2 = null ;
        public MyList()
        {
           this.frame.setLayout(new GridLayout(1,3)) ;
           String brother[] = {"大哥","呆萌","小三","小四","小五","大哥","呆萌","小三","小四","小五"} ;
           Vector<String> v = new Vector<String>() ;
           v.add("春天") ;
           v.add("夏天") ;
           v.add("秋天") ;
           v.add("冬天") ;
           this.list1 = new JList(brother) ;
           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.con.add(new JScrollPane(this.list1)) ;
           this.con.add(this.list2) ;
          
           this.frame.setSize(300,300) ;
           this.frame.setVisible(true) ;
          
           this.frame.addWindowListener(new WindowAdapter()
           {
               public void windowClosing(WindowEvent e)
               {
                   System.exit(1) ;
               }
           }) ;
        }
    }
    
    class Tester
    {
       public static void main(String args[])
       {
          new MyList() ;
       }
    }</span>



    常用监听:


    <span style="font-size:18px;">import java.util.Vector ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.WindowAdapter ;
    import java.awt.event.WindowListener ;
    import javax.swing.event.ListSelectionListener ;
    import javax.swing.event.ListSelectionEvent ;
    import java.awt.Container ;
    import java.awt.GridLayout ;
    import javax.swing.JFrame ;
    import javax.swing.JList ;
    import javax.swing.JScrollPane ;
    import javax.swing.BorderFactory ;
    import javax.swing.AbstractListModel ;
    import javax.swing.ListSelectionModel ;
    
    class MyListModel extends AbstractListModel
    {
       private String brothers[] = {"大哥","呆萌","小三","小四","小五"} ;
       public Object getElementAt(int index)
       {
          if(index<brothers.length)
          return brothers[index] ;
          else return null ;
       }
      
       public int getSize()
       {
          return brothers.length ;
       }
    }
    
    class MyList implements ListSelectionListener
    {
       JFrame frame = new JFrame() ;
       Container con = frame.getContentPane() ;
       private JList list = null ;
       public MyList()
       {
          this.frame.setLayout(new GridLayout(1,3)) ;
          this.list = new JList(new MyListModel()) ;
          this.list.setBorder(BorderFactory.createTitledBorder("你排行老几啊?")) ;
          this.list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) ;
          this.con.add(new JScrollPane(list)) ;
         
          this.frame.setSize(400,400) ;
          this.frame.setVisible(true) ;
          this.list.addListSelectionListener(this) ;
         
          this.frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent a){
                    System.exit(1) ;
                }
            }) ;
       }
      
       public void valueChanged(ListSelectionEvent e)
       {
           int temp[] = list.getSelectedIndices() ;
           //int temp[] = list1.getSelectedIndices() ;
           System.out.println("选中的内容是:") ;
           for(int i=0;i<temp.length;i++)
           {
              System.out.println(list.getModel().getElementAt(i)) ;
           }
       }
    }
    
    class Tester
    {
       public static void main(String args[])
       {
           new MyList() ;
       }
    }
    
    </span>


  • 相关阅读:
    Java使用POI操作Excel合并单元格
    LinkedList查询分析
    Redis面试题及答案
    分布式架构基础:Java RMI详解
    什么是线程
    ehcache、memcache、redis三大缓存比较
    ehcache入门基础示例
    js 异步提交文件
    .net core Model对象转换为uri网址参数形式
    net core2.1 在过滤器中获取post的body参数
  • 原文地址:https://www.cnblogs.com/emoji/p/4436806.html
Copyright © 2011-2022 走看看