Java JList的使用和事件响应

   我们选择文件时,会经常用到列表框和下拉列表框,二者在使用上会稍有不同。今天就先和大家分享下JList列表框的简单应用。

   JList最基本的功能是显示代表目录的字符串。作为一个控件,它经常被放到滚动条面板JScrollPane而不是面板JPanel。

//支持原创,转载请注明出处!

JList list = new JList();
JScrollPane scrollPane = new JScrollPane(list, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);//创建滚动条组件,默认滚动条始终出现,初始化列表组件
JList创建方式,常见的有通过数组,Vector,ListModel等等。这儿就不一一列举了,题主今天要是用的是faultListModel方式。

创建DataModdel类,它的父类DefaultListModel继承了AbstaractListModel抽象类,自然使用起来也更加方便。下面是创建该类的方法
class DataModel extends DefaultListModel
    {
        String[] s = {"  红色                         ","  绿色 ","  蓝色","  黑色","  黄色"};

        public DataModel()
        {
            for (int i = 0; i < s.length; i++) 
            {
                addElement(s[i]);//添加元素
            }
        }
    }

}

当我们选择“红色”列表项时,右侧面板的背景色就变成了红色
当然,我们选择别的颜色时,右侧面板的背景色也会相应有所变化
JList列表的创建和事件响应:
JList的事件响应可以使用列表框的事件处理方式,即ListSelectionListener。当然也可以使用组件的鼠标响应,这儿,咱们使用的就是鼠标适配器,单击列表项时就会有响应。

 model = new DataModel(); //创建DataModel对象
        JList list = new JList(model); //创建带有model的JList
        list.setBorder(BorderFactory.createTitledBorder("请选择一种你喜欢的颜色")); //设置列表框标题
        list.addMouseListener(new MouseAdapter() //列表框添加鼠标事件
        {
            public void mousePressed(MouseEvent e) 
            {
                int i = list.getSelectedIndex() + 1;
                Object path[] = model.toArray();
                if(i == 1)
                {
                    pane.setBackground(Color.red); //设置背景色
                }
                if(i == 2)
                {
                    pane.setBackground(Color.GREEN);
                }
                if(i == 3)
                {
                    pane.setBackground(Color.BLUE);
                }
                if(i == 4)
                {
                    pane.setBackground(Color.BLACK);
                }
                if(i == 5)
                {
                    pane.setBackground(Color.YELLOW);
                }

            }
        });

好的,代码分享最可爱的你

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

public class ListTestFrame extends JFrame {

    private JPanel contentPane,pane;
    private DataModel model;

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run() 
            {
                try
                {
                    ListTestFrame frame = new ListTestFrame();
                    frame.setVisible(true);
                } catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ListTestFrame() {
        setTitle("列表框 -调色器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(600, 300, 750, 500);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());

        pane = new JPanel();
        contentPane.add(pane, BorderLayout.CENTER);

        model = new DataModel(); //创建DataModel对象
        JList list = new JList(model); //创建带有model的JList
        list.setBorder(BorderFactory.createTitledBorder("请选择一种你喜欢的颜色")); //设置列表框标题
        list.addMouseListener(new MouseAdapter() //列表框添加鼠标事件
        {
            public void mousePressed(MouseEvent e) 
            {
                int i = list.getSelectedIndex() + 1;
                Object path[] = model.toArray();
                if(i == 1)
                {
                    pane.setBackground(Color.red); //设置背景色
                }
                if(i == 2)
                {
                    pane.setBackground(Color.GREEN);
                }
                if(i == 3)
                {
                    pane.setBackground(Color.BLUE);
                }
                if(i == 4)
                {
                    pane.setBackground(Color.BLACK);
                }
                if(i == 5)
                {
                    pane.setBackground(Color.YELLOW);
                }

            }
        });
        JScrollPane scrollPane = new JScrollPane(list, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); //创建带有JList对象的滚动条面板
        contentPane.add(scrollPane, BorderLayout.WEST);    
    }

    /*
     * 内部类,用以创建DataModel对象
     */
    class DataModel extends DefaultListModel
    {
        String[] s = {"  红色                         ","  绿色 ","  蓝色","  黑色","  黄色"};

        public DataModel()
        {
            for (int i = 0; i < s.length; i++) 
            {
                addElement(s[i]);//添加元素
            }
        }
    }

}

怎么样,si不si对你有所帮助,支持原创,尽情点赞,评论,转载和关注吧。

猜你喜欢

转载自blog.csdn.net/MusicEnchanter/article/details/78481368