列表框空间JList的用法演示

2024-06-23 18:58

本文主要是介绍列表框空间JList的用法演示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

package 列表框控件演示;import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;public class MyFrame extends JFrame implements ActionListener {//下面是DefaultListModel的私有变量private DefaultListModel sourceModel;private DefaultListModel destModel;//创建两个JList类型的变量private JList source;			//尚未初始化private JList dest = new JList();//初始化//创建两个按钮对象private JButton addButton = new JButton(">>");private JButton removeButton = new JButton("<<");//有参数的构造函数public MyFrame(String title) {super(title);//把此字符串传递给父类的构造函数显示在窗体的标题栏//好在SWing API的设计者想出了用于事件处理的适配器Adapter类,从而省却实现众多接口方法的麻烦,以下是匿名内部类addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){//实现此方法,以此来优雅的终止程序System.exit(0);}});//创建DufaultListModel类的一个实例sourceModel = new DefaultListModel();//向创建的数据模型中添加几个String类型的选项sourceModel.addElement("Banana");sourceModel.addElement("Apple");sourceModel.addElement("Orange");sourceModel.addElement("Mango");sourceModel.addElement("Pineapple");sourceModel.addElement("Kiwi");sourceModel.addElement("Strawberry");sourceModel.addElement("Peach");//创建一个JList实例然后将先前创建的数据模型作为参数传递给JList的构造函数source = new JList(sourceModel);//这种先前没有实例化的//为源列表框设置选择模式:单选模式source.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//source.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5),""+"Shop",0,0,null,Color.RED));//设置初始化列表框中选中的第一个选项source.setSelectedIndex(0);source.setSelectionBackground(Color.BLACK);source.setSelectionForeground(Color.WHITE);//创建另一个列表框的数据模型destModel = new DefaultListModel();dest.setModel(destModel);//这种是先前创建好了实例对象的dest.setSelectionBackground(Color.BLACK);dest.setSelectionForeground(Color.WHITE);dest.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5),""+"Fruit Basket",0,0,null,Color.RED));//提示:通过创建数据模型,允许在程序运行时替换列表框中的内容//建立GUI:用户界面的创建//使用面板的目的是用作放置组件的容器JPanel panel = new JPanel();//为面板设置布局管理器为网格布局4行1列panel.setLayout(new GridLayout(4,1,20,20));panel.add(new JLabel());//第1行为空的即标签panel.add(addButton);panel.add(removeButton);panel.add(new JLabel());//最后一行为空的即用标签来显示在面板上//为窗口设置布局管理器为网格布局1行3列this.setLayout(new GridLayout(1,3,20,20));add(source);add(panel);add(dest);//注册事件监听器:这是最后需要完成的一件事情,就是为按钮添加事件监听器addButton.addActionListener(this);removeButton.addActionListener(this);}//以下是按钮的事件处理代码@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource().equals(addButton)){if(source.getSelectedValue()!=null)//调用JList类的getSelectedValue()方法{String str=(String)source.getSelectedValue();if(str!=null){destModel.addElement(str);dest.setSelectedIndex(0);sourceModel.removeElement(str);source.setSelectedIndex(0);}}}if(e.getSource().equals(removeButton)){if(dest.getSelectedValue()!=null){String str=(String)dest.getSelectedValue();if(str!=null){sourceModel.addElement(str);source.setSelectedIndex(0);destModel.removeElement(str);dest.setSelectedIndex(0);}}}}}

下面是测试的类:

package 列表框控件演示;public class ListDemoApp {public static void main(String[] args) {// TODO Auto-generated method stubMyFrame frame = new MyFrame("list Demo");frame.setBounds(20,50,400,300);//(x,y,width,heigth)//frame.setSize(400,300);(width,heigth)frame.setVisible(true);}}


这篇关于列表框空间JList的用法演示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1087996

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

Linux环境变量&&进程地址空间详解

《Linux环境变量&&进程地址空间详解》本文介绍了Linux环境变量、命令行参数、进程地址空间以及Linux内核进程调度队列的相关知识,环境变量是系统运行环境的参数,命令行参数用于传递给程序的参数,... 目录一、初步认识环境变量1.1常见的环境变量1.2环境变量的基本概念二、命令行参数2.1通过命令编程

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

关于Maven生命周期相关命令演示

《关于Maven生命周期相关命令演示》Maven的生命周期分为Clean、Default和Site三个主要阶段,每个阶段包含多个关键步骤,如清理、编译、测试、打包等,通过执行相应的Maven命令,可以... 目录1. Maven 生命周期概述1.1 Clean Lifecycle1.2 Default Li

oracle中exists和not exists用法举例详解

《oracle中exists和notexists用法举例详解》:本文主要介绍oracle中exists和notexists用法的相关资料,EXISTS用于检测子查询是否返回任何行,而NOTE... 目录基本概念:举例语法pub_name总结 exists (sql 返回结果集为真)not exists (s

Springboot中Jackson用法详解

《Springboot中Jackson用法详解》Springboot自带默认json解析Jackson,可以在不引入其他json解析包情况下,解析json字段,下面我们就来聊聊Springboot中J... 目录前言Jackson用法将对象解析为json字符串将json解析为对象将json文件转换为json