• java swing中对于JList的使用(二)


    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;
        }
    
    }
    


     
  • 相关阅读:
    etl接口测试总结
    LR controller 参数化
    Mysql导入导出命令
    mysql基本命令
    mysql用户管理(新增用户及权限管理)
    源码编译搭建LAMP
    【OpenGL】Shader实例分析(一)-Wave
    仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
    cocos2d-x游戏开发(十四)用shader使图片背景透明
    泰然发布了一系列 OpenGL3.0 的教程,推荐大家参考学习
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986894.html
Copyright © 2020-2023  润新知