zoukankan      html  css  js  c++  java
  • 列表选择模式:单选、按shift、按shift或ctrl

    2018-10-29 21:23:16 开始写



     1 import java.awt.BorderLayout;
     2 import java.awt.EventQueue;
     3 import javax.swing.JFrame;
     4 import javax.swing.JPanel;
     5 import javax.swing.border.EmptyBorder;
     6 import javax.swing.UIManager;
     7 import java.awt.GridLayout;
     8 import javax.swing.JScrollPane;
     9 import javax.swing.JList;
    10 import java.awt.event.WindowAdapter;
    11 import java.awt.event.WindowEvent;
    12 import javax.swing.ListSelectionModel;
    13 import javax.swing.JLabel;
    14 import javax.swing.SwingConstants;
    15 import java.awt.Font;
    16 import javax.swing.JFrame;
    17 
    18 public class JListSelectModelTest extends JFrame {
    19     private JPanel contentPane;
    20     private JList list1;
    21     private JList list2;
    22     private JList list3;
    23     private JLabel label1;
    24     private JLabel label2;
    25     private JLabel label3;
    26     public static void main(String[] args) {
    27         // TODO Auto-generated method stub
    28         try {
    29             UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    30         } catch (Throwable e) {
    31             e.printStackTrace();
    32         }
    33         JListSelectModelTest frame = new JListSelectModelTest();
    34         frame.setVisible(true);
    35     }
    36     
    37     public JListSelectModelTest() {
    38         setTitle("列表项的选择模式");
    39         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    40         setBounds(100, 100, 550, 300);
    41         contentPane = new JPanel();
    42         contentPane.setLayout(new BorderLayout(0, 0));
    43         setContentPane(contentPane);
    44         
    45         JPanel panel = new JPanel();
    46         contentPane.add(panel, BorderLayout.CENTER);
    47         panel.setLayout(new GridLayout(1, 3, 5, 5));
    48         
    49         JScrollPane scrollPane1 = new JScrollPane();
    50         panel.add(scrollPane1);
    51         
    52         list1 = new JList();
    53         list1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
    54         list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//设置选择模式为单选
    55         scrollPane1.setViewportView(list1);
    56         
    57         label1 = new JLabel("单项选择");
    58         label1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
    59         label1.setHorizontalAlignment(SwingConstants.CENTER);//设置标签水平居中
    60         scrollPane1.setColumnHeaderView(label1);//设置滑动面板首个显示
    61         
    62         JScrollPane scrollPane2 = new JScrollPane();
    63         panel.add(scrollPane2);
    64         
    65         list2 = new JList();
    66         list2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
    67         list2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);//设置选择模式为单选或shift
    68         scrollPane2.setViewportView(list2);
    69         
    70         label2 = new JLabel("连续选择(按shift)");
    71         label2.setHorizontalAlignment(SwingConstants.CENTER);
    72         label2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
    73         scrollPane2.setColumnHeaderView(label2);
    74         
    75         JScrollPane scrollPane3 = new JScrollPane();
    76         panel.add(scrollPane3);
    77 
    78         list3 = new JList();
    79         list3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
    80         list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//设置选择模式为单选或shift或ctrl
    81         scrollPane3.setViewportView(list3);
    82 
    83         label3 = new JLabel("默认选择(按Ctrl/Shift)");
    84         label3.setHorizontalAlignment(SwingConstants.CENTER);
    85         label3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
    86         scrollPane3.setColumnHeaderView(label3);
    87         
    88         String[] listData = new String[12];
    89         for (int i = 0; i < listData.length; i++) {
    90             listData[i] = "余杰" + (i + 1);
    91         }
    92         list1.setListData(listData);//添加列表元素
    93         list2.setListData(listData);
    94         list3.setListData(listData);
    95     }
    96 }

    说明

    单选:一次选中一个

    shift:多个连续

    ctrl:选中多个(不用连续)


    谢谢、Thank you、Salamat Do(撒拉玛特朵)、あリがCám o*n(嘉蒙)とゥ(阿里嘎都)、감사합니다 (勘三哈咪瘩)、terima Kasih(得力马卡系)、kob-khun(寇布库恩)、dhanyavaad(达尼阿瓦德)、toda(透达)、te?ekkür ederim(特谢库尔埃戴里姆)、Dzie,kuje(以库以而)、Spasibo(思巴喜柏)、Danke(但可)、shokran(休克朗)、Obrigada(欧布哩嘎搭)、dank u(当Q)、Merci(梅呵西)、Gracias(格拉喜亚思)、Grazie(格啦姬)、Благодарности、Tak、Kiitos、Díky、Hvala、Mul?umesc、Takk、Ευχαριστ

  • 相关阅读:
    IDEA效率快捷键
    常用文件/文件夹操作
    git log状态下退出方法
    ImageList 构造函数
    ImageList 控件
    【转】图像分割代码合集
    【转】图像分割论文及代码资源汇总
    SLIC 算法
    c/c++内存分配详解
    c++内存分配
  • 原文地址:https://www.cnblogs.com/YUJIE666/p/9873478.html
Copyright © 2011-2022 走看看