• java Swing中对于JList控件的使用(一)


     
    package com.robert;
    
    import javax.swing.*;
    import java.awt.*;
    
    /**
     * Created by IntelliJ IDEA.
     * User: Administrator
     * Date: 11-11-12
     * Time: 下午7:14
     * To change this template use File | Settings | File Templates.
     */
    public class ListTest {
        public static void main(String args[])
        {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new ListFrame();
                    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 java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     * Created by IntelliJ IDEA.
     * User: Administrator
     * Date: 11-11-12
     * Time: 下午7:16
     * To change this template use File | Settings | File Templates.
     */
    
    public class ListFrame extends JFrame {
    
        private static int DEFAULT_WIDTH = 400;
        private static int DEFAULT_HEIGHT = 300;
        private JPanel listPanel;
        private JList wordList;
        private JLabel label;
        private JPanel buttonPanel;
        private ButtonGroup group;
        private String prefix = "The ";
        private String suffix = "fox jumps over the lazy dog";
    
        public ListFrame()
        {
            setTitle("ListTest");
            setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    
            String[] words= {"quick","brown","hungry","wild",
                    "silent","huge","private","abstract","static","final"};
            wordList = new JList(words);
            wordList.setVisibleRowCount(6);
    
            JScrollPane scrollPane = new JScrollPane(wordList);
            listPanel = new JPanel();
            listPanel.add(scrollPane);
            wordList.addListSelectionListener(new ListSelectionListener() {
    
                public void valueChanged(ListSelectionEvent e) {
                    Object[] values = wordList.getSelectedValues();
    
                    StringBuilder text = new StringBuilder(prefix);
                    for(int i=0;i<values.length;i++)
                    {
                        String word = (String)values[i];
                        text.append(word);
                        text.append(" ");
                    }
                    text.append(suffix);
                    label.setText(text.toString());
                }
            });
    
            buttonPanel = new JPanel();
            group = new ButtonGroup();
            makeButton("Vertical",JList.VERTICAL);
            makeButton("Vertical Wrip",JList.VERTICAL_WRAP);
            makeButton("Horizonta Wrap",JList.HORIZONTAL_WRAP);
    
            add(listPanel, BorderLayout.NORTH);
            label = new JLabel(prefix + suffix);
            add(label, BorderLayout.CENTER);
            add(buttonPanel,BorderLayout.SOUTH);
        }
    
        private void makeButton(String label, final int orientation ) {
            JRadioButton button = new JRadioButton(label);
            buttonPanel.add(button);
            if(group.getButtonCount()==0)
            {
                button.setSelected(true);
            }
            group.add(button);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    wordList.setLayoutOrientation(orientation);
                    listPanel.revalidate();
                }
            });
        }
    
    }
    



     

  • 相关阅读:
    ArcGis Desktop arcmap安装步骤
    ArcMap 中添加 ArcGis server
    arcgis server 安装
    哨兵数据处理,哨兵数据水体数据提取
    Web安全之SQL注入攻击技巧与防范(转)
    干货分享丨Python从入门到编写POC之初识OOP和类
    干货分享丨Python从入门到编写POC之爬虫专题
    干货分享丨XSS利用进阶
    CVE-2020-9484 Apache Tomcat反序列化漏洞浅析
    干货分享丨Python从入门到编写POC之常用的标准库
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986895.html
Copyright © 2020-2023  润新知