列表选择模式:单选、按shift、按shift或ctrl
程序员文章站
2022-08-12 09:48:21
2018-10-29 21:23:16 开始写 说明 单选:一次选中一个 shift:多个连续 ctrl:选中多个(不用连续) 谢谢、Thank you、Salamat Do(撒拉玛特朵)、あリがCám o*n(嘉蒙)とゥ(阿里嘎都)、감사합니다 (勘三哈咪瘩)、terima Kasih(得力马卡系 ......
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、ευχαριστ
上一篇: 秒变拍照达人 优秀人像摄影手机推荐
下一篇: 爆囧,熊孩子气死人