码迷,mamicode.com
首页 > 其他好文 > 详细

列表框JList 及动作监听

时间:2015-04-07 12:04:02      阅读:223      评论:0      收藏:0      [点我收藏+]

标签: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>


列表框JList 及动作监听

标签:jlist

原文地址:http://blog.csdn.net/u012566693/article/details/44916939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!