【Java-GUI】12 Swing07 JList

列表和下拉选择:

package cn.dzz;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class JList {
    JFrame jFrame = new JFrame("列表框测试");
    String[] bookList = {
            "java自学宝典",
            "轻量级JavaEE企业级开发",
            "android基础教程",
            "jquery实战教程",
            "SpringBoot企业级开发"
    };

    JPanel layoutPanel = new JPanel();
    ButtonGroup layoutGroup = new ButtonGroup();

    JPanel selectModePanel = new JPanel();
    ButtonGroup selectModeGroup = new ButtonGroup();
    JTextArea displayZone = new JTextArea(4, 40);

    // JList对象
    javax.swing.JList<String> jBookList;
    JComboBox<String> bookSelector;

    public void init() {


        jBookList = new javax.swing.JList(bookList);

        addBtn2LayoutPanel("纵向滚动", javax.swing.JList.VERTICAL );
        addBtn2LayoutPanel("纵向换行", javax.swing.JList.VERTICAL_WRAP );
        addBtn2LayoutPanel("横向滚动", javax.swing.JList.HORIZONTAL_WRAP );

        addBtn2SelectModelPanel("无限制", ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        addBtn2SelectModelPanel("单选", ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        addBtn2SelectModelPanel("单范围", ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        
        jBookList.setVisibleRowCount(3);
        jBookList.setSelectionInterval(2, 4);

        // 选中处理
        jBookList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                List<String> selectedValuesList = jBookList.getSelectedValuesList();
                displayZone.setText("");
                for (String s : selectedValuesList) {
                    displayZone.append(s + "
");
                }
            }
        });
        

        Box verticalBox = Box.createVerticalBox();
        verticalBox.add(new JScrollPane(jBookList));
        verticalBox.add(layoutPanel);
        verticalBox.add(selectModePanel);

        Vector<String> vector = new Vector<>();
        List<String> list = Arrays.asList(bookList);
        vector.addAll(list);

        bookSelector = new JComboBox<>(vector);
        bookSelector.setEditable(true);
        bookSelector.setMaximumRowCount(4);

        // 监听下拉选择
        bookSelector.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                Object selectedItem = bookSelector.getSelectedItem();
                displayZone.setText(selectedItem.toString() + "
");
            }
        });

        Box horizontalBox = Box.createHorizontalBox();
        horizontalBox.add(verticalBox);
        //horizontalBox.add(bookSelector);
        JPanel bookSelectorPanel = new JPanel();
        bookSelectorPanel.add(bookSelector);
        horizontalBox.add(bookSelectorPanel);


        JPanel bottomPanel = new JPanel();
        bottomPanel.add(new JLabel("您最喜欢的图书:"), BorderLayout.NORTH);
        bottomPanel.add(displayZone);

        Box verticalBox1 = Box.createVerticalBox();
        verticalBox1.add(horizontalBox);
        verticalBox1.add(bottomPanel);

        jFrame.add(verticalBox1);

        // bookSelector = new JComboBox<>(books);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    // 封装按钮
    public void addBtn2LayoutPanel(String name, int layoutType) {
        layoutPanel.setBorder(new TitledBorder(new EtchedBorder(), "确定选项布局"));
        JRadioButton button = new JRadioButton(name);
        layoutPanel.add(button);

        if (layoutGroup.getButtonCount() == 0) {
            button.setSelected(true);
        }

        layoutGroup.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jBookList.setLayoutOrientation(layoutType);
            }
        });
    }

    public void addBtn2SelectModelPanel(String name, int selectionModel) {
        selectModePanel.setBorder(new TitledBorder(new EtchedBorder(), "确定选项模式"));
        JRadioButton button = new JRadioButton(name);
        selectModePanel.add(button);

        if (selectModeGroup.getButtonCount() == 0) {
            button.setSelected(true);
        }

        selectModePanel.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jBookList.setSelectionMode(selectionModel);
            }
        });
    }
    
    public static void main(String[] args) {
        new JList().init();
    }
}

原文地址:https://www.cnblogs.com/mindzone/p/14517447.html