博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java swing中对于JList的使用(二)
阅读量:6432 次
发布时间:2019-06-23

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

package com.robert;import javax.swing.*;import java.awt.*;/** * Created by IntelliJ IDEA. * User: Administrator * Date: 11-11-12 * Time: 下午8:53 * To change this template use File | Settings | File Templates. */public class LongListTest {    public static void main(String[] args)    {        EventQueue.invokeLater(new Runnable() {            public void run() {                JFrame frame = new LongListTestFrame();                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                frame.setVisible(true);            }        });    }}
package com.robert;import javax.swing.*;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.xml.soap.Text;import java.awt.*;/** * Created by IntelliJ IDEA. * User: Administrator * Date: 11-11-12 * Time: 下午8:55 * To change this template use File | Settings | File Templates. */public class LongListTestFrame extends JFrame{    private static final int DEFAULT_WIDTH = 400;    private static final int DEFAULT_HEIGHT = 300;    private JList wordList;    private JLabel label;    private String prefix = "The quick brown ";    private String suffix = " jump over the lazy dog.";    public LongListTestFrame()    {        setTitle("LongListTest");        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);        wordList = new JList(new WordListModel(3));        wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);        wordList.setPrototypeCellValue("www");        JScrollPane scrollPane = new JScrollPane(wordList);        JPanel p = new JPanel();        p.add(scrollPane);        wordList.addListSelectionListener(new ListSelectionListener() {            public void valueChanged(ListSelectionEvent e) {                StringBuilder word = (StringBuilder)wordList.getSelectedValue();                setSubject(word.toString());            }        });        Container contentPane = getContentPane();        contentPane.add(p,BorderLayout.NORTH);        label = new JLabel(prefix + suffix);        contentPane.add(label, BorderLayout.CENTER);        setSubject("FOX");    }    public void setSubject(String word)    {        StringBuilder text = new StringBuilder(prefix);        text.append(word);        text.append(suffix);        label.setText(text.toString());    }}
package com.robert;import com.sun.org.apache.bcel.internal.generic.LASTORE;import javax.swing.*;/** * Created by IntelliJ IDEA. * User: Administrator * Date: 11-11-12 * Time: 下午9:03 * To change this template use File | Settings | File Templates. */public class WordListModel extends AbstractListModel {    private int length;    public static final char FIRST = 'a';    public static final char LAST = 'z';    public WordListModel(int n)    {        length = n;    }    public int getSize()    {        return (int)Math.pow(LAST - FIRST + 1,length);    }    public Object getElementAt(int n)    {        StringBuilder r = new StringBuilder();        for(int i = 0; i < length; i++)        {            char c = (char)(FIRST + n % (LAST - FIRST + 1));            r.insert(0, c);            n = n / (LAST - FIRST + 1);        }        return r;    }}
 

转载于:https://www.cnblogs.com/mengjianzhou/archive/2011/11/12/5986894.html

你可能感兴趣的文章
poj 3414 Pots (bfs+线索)
查看>>
Binary search
查看>>
http://jingyan.baidu.com/article/08b6a591f0fafc14a9092275.html
查看>>
MySQL查询数据表的Auto_Increment(自增id)
查看>>
java多线程系类:JUC集合:01之框架
查看>>
【Linux】 源码安装make命令详解,避免踩坑
查看>>
数据库中间表插入乱序
查看>>
[Python爬虫] 之四:Selenium 抓取微博数据
查看>>
使用OPENROWSET爆破SQL Server密码
查看>>
Mac_安装Homebrew以及Maven
查看>>
eclipse web开发Server配置
查看>>
曹政--互联网搜索老师傅
查看>>
MUI框架开发HTML5手机APP(一)--搭建第一个手机APP(转)
查看>>
linux下使用 du查看某个文件或目录占用磁盘空间的大小
查看>>
Android水波纹特效的简单实现
查看>>
MugLife静态照片变3D动画算法研究
查看>>
[wp7软件]wp7~~各种视频播放器下载大全
查看>>
基于NodeJS的HTTP server Plus 4:多语言(Accept-Language/Content-Language)
查看>>
详解 Vue 生命周期实现
查看>>
Java工程师必知之事 —— 如何定义自己的职业路线?
查看>>