JListDemo

Tips:

(1)JList不能自动滚动,要想为列表框加上滚动条,必须将JList插入到一个JScrollPane中,然后将JScollPane而不是JList,插入到外围JPanel上

(2)ListSelectionListener
public void valueChanged(ListSelectionEvent event)
在用户选择了若干个选项的同时,将产生一系统列表选择事件。假如用户在一个新选项上单击,当鼠标按下的时候,就会有一个事件报告选项的改变。这是一种过渡型事件。在调用event.isAdjusting()时,如果该选择仍未最终结束则返回true.然后,当松开鼠标时,就产生另一事件,此时isAdjusting返回false.如果对过渡事件不感兴趣,那么可以等待isAdjusting调用返回false的事件。
不过,如果希望只要点击鼠标就给用户一个即时反馈,那么就需要处理所有的事件。

(3)JList不响应鼠标的双击事件。正如Swing设计者所构想的那样,使用列表选择一个选项,然后点击某个按钮执行某个动作。
如果想让JList对双击事件进行响应,必须对JList添加一个鼠标监听器,然后按照下面这样捕获鼠标事件:
eg:

        wordList.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==2) {
                    JList source=(JList) e.getSource();
                    Object[] selection=source.getSelectedValues();
                    doAction(selection);
                }
            }
        });

package swing.jlist;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*2015-7-8*/
public class JListTest {
    public static void main(String[] args) {
        ListFrame frame = new ListFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

}

class ListFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 300;

    private JPanel listPanel;
    private JList wordList;
    private JLabel label;
    private JPanel buttonPanel;
    private ButtonGroup group;
    private String prefix = "The ";
    private String suffix = "fox jumps over the lazy dog.";

    public ListFrame() {
        setTitle("ListTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        String[] words = { "quick", "brown", "hungry", "wild", "silent", "huge", "private", "abstract", "static", "final" };
        wordList = new JList(words);
        wordList.setVisibleRowCount(4);
        JScrollPane scrollPane = new JScrollPane(wordList);
        listPanel = new JPanel();
        listPanel.add(scrollPane);
        wordList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                Object[] values = wordList.getSelectedValues();
                StringBuilder text = new StringBuilder(prefix);
                for (int i = 0; i < values.length; i++) {
                    String word = (String) values[i];
                    text.append(word);
                    text.append("  ");
                }
                text.append(suffix);
                label.setText(text.toString());
            }
        });

        buttonPanel = new JPanel();
        group = new ButtonGroup();
        makeButton("Vertical", JList.VERTICAL);
        makeButton("VERTICAL_WRAP", JList.VERTICAL_WRAP);
        makeButton("HORIZONTAL_WRAP", JList.HORIZONTAL_WRAP);
        add(listPanel, BorderLayout.NORTH);
        label = new JLabel(prefix + suffix);
        add(label, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

    }

    private void makeButton(String label, final int orientation) {
        JRadioButton button = new JRadioButton(label);
        buttonPanel.add(button);
        if (group.getButtonCount() == 0) {
            button.setSelected(true);
        }

        group.add(button);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                wordList.setLayoutOrientation(orientation);
                listPanel.revalidate();
            }
        });

    }

}
时间: 2024-10-09 06:22:13

JListDemo的相关文章

java Swing 之列表框控件

/** * java Swing 列表框控件 * @author gao */ package com.gao; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class JListD