• 列表框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>


  • 相关阅读:
    PHP-FPM 重启
    white-space: nowrap
    php-fpm 优化
    & 引用传值
    tp3 save操作小bug误区
    用百度接口验证是否上传了身份证图片信息[非姓名,身份证号匹配]
    nginx 反向代理案例
    IOS把图片缓存到本地的几种方法
    ios沙盒查找图片展示
    iOS模拟器沙盒使用推荐
  • 原文地址:https://www.cnblogs.com/emoji/p/4436806.html
Copyright © 2020-2023  润新知