📜  Java摇摆 |带有示例的 JList

📅  最后修改于: 2022-05-13 01:55:45.931000             🧑  作者: Mango

Java摇摆 |带有示例的 JList

JList 是Java Swing 包的一部分。 JList 是一个显示一组对象并允许用户选择一个或多个项目的组件。 JList 继承 JComponent 类。 JList 是一种显示 Vectors 数组的简单方法。
JList 的构造函数是:

  1. JList() : 创建一个空的空白列表
  2. JList(E [ ] l) :创建一个包含数组元素的新列表。
  3. JList(ListModel d) :使用指定的列表模型创建一个新列表
  4. JList(Vector l) :使用向量的元素创建一个新列表

常用的方法有:

methodexplanation
getSelectedIndex()returns the index of selected item of the list
getSelectedValue()returns the selected value of the element of the list
setSelectedIndex(int i)sets the selected index of the list to i
setSelectionBackground(Color c)sets the background Color of the list
setSelectionForeground(Color c)Changes the foreground color of the list
setListData(E [ ] l)Changes the elements of the list to the elements of l .
setVisibleRowCount(int v)Changes the visibleRowCount property
setSelectedValue(Object a, boolean s)selects the specified object from the list.
setSelectedIndices(int[] i)changes the selection to be the set of indices specified by the given array.
setListData(Vector l)constructs a read-only ListModel from a Vector specified.
setLayoutOrientation(int l)defines the orientation of the list
setFixedCellWidth(int w)Changes the cell width of list to the value passed as parameter.
setFixedCellHeight(int h)Changes the cell height of the list to the value passed as parameter.
isSelectedIndex(int i)returns true if the specified index is selected, else false.
indexToLocation(int i)returns the origin of the specified item in the list’s coordinate system.
getToolTipText(MouseEvent e)returns the tooltip text to be used for the given event.
getSelectedValuesList()returns a list of all the selected items.
getSelectedIndices()returns an array of all of the selected indices, in increasing order
getMinSelectionIndex()returns the smallest selected cell index, or -1 if the selection is empty.
getMaxSelectionIndex()returns the largest selected cell index, or -1 if the selection is empty.
getListSelectionListeners()returns the listeners of list
getLastVisibleIndex()returns the largest list index that is currently visible.
getDragEnabled()returns whether or not automatic drag handling is enable
addListSelectionListener(ListSelectionListener l)adds a listSelectionlistener to the list

以下程序将说明 JLists 的使用
1.程序创建一个简单的JList

Java
// java Program to create a simple JList
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame
{
     
    //frame
    static JFrame f;
     
    //lists
    static JList b;
  
 
    //main class
    public static void main(String[] args)
    {
        //create a new frame
        f = new JFrame("frame");
         
        //create a object
        solve s=new solve();
       
        //create a panel
        JPanel p =new JPanel();
         
        //create a new label
        JLabel l= new JLabel("select the day of the week");
 
        //String array to store weekdays
        String week[]= { "Monday","Tuesday","Wednesday",
                         "Thursday","Friday","Saturday","Sunday"};
         
        //create list
        b= new JList(week);
         
        //set a selected index
        b.setSelectedIndex(2);
         
        //add list to panel
        p.add(b);
  
        f.add(p);
         
        //set the size of frame
        f.setSize(400,400);
          
        f.show();
    }
     
     
}


Java
// java Program to create a list and add itemListener to it
// (program to select your birthday using lists) .
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ListSelectionListener
{
     
    //frame
    static JFrame f;
     
    //lists
    static JList b,b1,b2;
     
    //label
    static JLabel l1;
  
 
    //main class
    public static void main(String[] args)
    {
        //create a new frame
        f = new JFrame("frame");
         
        //create a object
        solve s=new solve();
       
        //create a panel
        JPanel p =new JPanel();
         
        //create a new label
        JLabel l= new JLabel("select your birthday");
        l1= new JLabel();
 
        //String array to store weekdays
        String month[]= { "January", "February", "March",
        "April", "May", "June", "July", "August",
        "September", "October", "November", "December"};
         
        //create a array for months and year
        String date[]=new String[31],year[]=new String[31];
         
        //add month number and year to list
        for(int i=0;i<31;i++)
        {
            date[i]=""+(int)(i+1);
            year[i]=""+(int)(2018-i);
        }
         
        //create lists
        b= new JList(date);
        b1= new JList(month);
        b2= new JList(year);
         
        //set a selected index
        b.setSelectedIndex(2);
        b1.setSelectedIndex(1);
        b2.setSelectedIndex(2);
         
        l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
                              +" "+b2.getSelectedValue());
         
        //add item listener
        b.addListSelectionListener(s);
        b1.addListSelectionListener(s);
        b2.addListSelectionListener(s);
         
        //add list to panel
        p.add(l);
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(l1);
  
        f.add(p);
         
        //set the size of frame
        f.setSize(500,600);
          
        f.show();
    }
    public void valueChanged(ListSelectionEvent e)
    {
        //set the text of the label to the selected value of lists
        l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
                              +" "+b2.getSelectedValue());
         
    }
     
     
}


输出 :

2. 创建一个列表并将 itemListener 添加到它的程序(使用列表选择您的生日的程序)。

Java

// java Program to create a list and add itemListener to it
// (program to select your birthday using lists) .
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ListSelectionListener
{
     
    //frame
    static JFrame f;
     
    //lists
    static JList b,b1,b2;
     
    //label
    static JLabel l1;
  
 
    //main class
    public static void main(String[] args)
    {
        //create a new frame
        f = new JFrame("frame");
         
        //create a object
        solve s=new solve();
       
        //create a panel
        JPanel p =new JPanel();
         
        //create a new label
        JLabel l= new JLabel("select your birthday");
        l1= new JLabel();
 
        //String array to store weekdays
        String month[]= { "January", "February", "March",
        "April", "May", "June", "July", "August",
        "September", "October", "November", "December"};
         
        //create a array for months and year
        String date[]=new String[31],year[]=new String[31];
         
        //add month number and year to list
        for(int i=0;i<31;i++)
        {
            date[i]=""+(int)(i+1);
            year[i]=""+(int)(2018-i);
        }
         
        //create lists
        b= new JList(date);
        b1= new JList(month);
        b2= new JList(year);
         
        //set a selected index
        b.setSelectedIndex(2);
        b1.setSelectedIndex(1);
        b2.setSelectedIndex(2);
         
        l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
                              +" "+b2.getSelectedValue());
         
        //add item listener
        b.addListSelectionListener(s);
        b1.addListSelectionListener(s);
        b2.addListSelectionListener(s);
         
        //add list to panel
        p.add(l);
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(l1);
  
        f.add(p);
         
        //set the size of frame
        f.setSize(500,600);
          
        f.show();
    }
    public void valueChanged(ListSelectionEvent e)
    {
        //set the text of the label to the selected value of lists
        l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
                              +" "+b2.getSelectedValue());
         
    }
     
     
}

输出 :

注意:以上程序可能无法在在线编译器中运行,请使用离线 IDE