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

你可能感兴趣的:(java Swing中对于JList控件的使用(一))