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

}



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值