博客
关于我
Java Swing JList:列表框组件
阅读量:362 次
发布时间:2019-03-04

本文共 1068 字,大约阅读时间需要 3 分钟。

1 JList:列表框组件

列表框与下拉列表的区别不仅仅表现在外观上,当激活下拉列表时,会出现下拉列表框中的内容。但列表框只是在窗体系上占据固定的大小,如果需要列表框具有滚动效果,可以将列表框放到滚动面板中。当用户选择列表框中的某一项时,按住 Shift 键并选择列表框中的其他项目,可以连续选择两个选项之间的所有项目,也可以按住 Ctrl 键选择多个项目。

1.1 构造函数

JList():构造一个空的只读模型的列表框。JList(ListModel dataModel):根据指定的非 null 模型对象构造一个显示元素的列表框。JList(Object[] listData):使用 listData 指定的元素构造—个列表框。JList(Vector
listData):使用 listData 指定的元素构造一个列表框。

上述的第一个构造方法没有参数,使用此方法创建列表框后可以使用 setListData() 方法对列表框的元素进行填充,也可以调用其他形式的构造方法在初始化时对列表框的元素进行填充。常用的元素类型有 3 种,分别是数组、Vector 对象和 ListModel 模型。

1.2 示例

import javax.swing.*;public class JListDemo {       public static void main(String[] args) {           JFrame frame=new JFrame("Java列表框组件示例");        JPanel jp=new JPanel();    //创建面板        JLabel label1=new JLabel("证件类型:");    //创建标签        String[] items=new String[]{   "身份证","驾驶证","军官证"};        JList list=new JList(items);    //创建JList        jp.add(label1);        jp.add(list);        frame.add(jp);        frame.setBounds(300,200,400,100);        frame.setVisible(true);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }}

1.3 运行效果

在这里插入图片描述

转载地址:http://vnqr.baihongyu.com/

你可能感兴趣的文章
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>