就是QQ群里面用户列表那种效果,即每个用户用图标和文字表示,点击鼠标左键可以选定一个用户。应该用哪种组件来做,试了好多都不行。

解决方案 »

  1.   

    楼上的朋友能说的具体点吗?我是用java写聊天室,combox这个是下拉列表吧,它只能显示一个用户,好像不行的。
      

  2.   

    把你的代码发给我:[email protected]
      

  3.   

    import java.awt.*;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;
    import javax.swing.border.Border;public class UserListTest {

    public static void main(String[] args) {
    DefaultListModel model = new DefaultListModel();
    for (int i = 0; i < 50; i++) {
    model.addElement("User " + i);
    }
    final JList list = new JList(model);
    list.setCellRenderer(new BigIconListCellRenderer()); final JCheckBox smallIconCheckBox = new JCheckBox("Small Icon");
    smallIconCheckBox.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    if (smallIconCheckBox.isSelected()) {
    list.setCellRenderer(new SmallIconListCellRenderer());
    }
    else {
    list.setCellRenderer(new BigIconListCellRenderer());
    }
    }
    });
    JScrollPane sp = new JScrollPane(list);
    JFrame frame = new JFrame("UserListTest");
    frame.getContentPane().add(sp, BorderLayout.CENTER);
    frame.getContentPane().add(smallIconCheckBox, BorderLayout.NORTH);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    private static class BigIconListCellRenderer extends DefaultListCellRenderer {
    private JPanel pane = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
    Border focusBorder =
    BorderFactory.createCompoundBorder(
    UIManager.getBorder("List.focusCellHighlightBorder"),
    BorderFactory.createEmptyBorder(8,12,8,12));

    Border noFocusBorder =
    BorderFactory.createCompoundBorder(
    DefaultListCellRenderer.noFocusBorder,
    BorderFactory.createEmptyBorder(8,12,8,12));

    public BigIconListCellRenderer() {
    setHorizontalAlignment(CENTER);
    setVerticalTextPosition(BOTTOM);
    setHorizontalTextPosition(CENTER);
    pane.add(this);
    pane.setOpaque(false);
    }

    public Component getListCellRendererComponent(
    JList list, Object value, int index, boolean isSelected, 
    boolean cellHasFocus) {

    super.getListCellRendererComponent(
    list, value, index, isSelected, cellHasFocus);

    setIcon(TestIcon.BigIconInstance);
    setBorder((cellHasFocus) ? focusBorder : noFocusBorder);
    return pane;
    }
    } private static class SmallIconListCellRenderer extends DefaultListCellRenderer {
    public Component getListCellRendererComponent(
    JList list, Object value, int index, boolean isSelected, 
    boolean cellHasFocus) {

    super.getListCellRendererComponent(
    list, value, index, isSelected, cellHasFocus);

    setIcon(TestIcon.SmallIconInstance);
    return this;
    }
    }

    private static class TestIcon implements Icon {
    private static Icon BigIconInstance = new TestIcon();

    private static Icon SmallIconInstance = new TestIcon() {
    public int getIconHeight() {
    return 16;
    }

    public int getIconWidth() {
    return 16;
    }
    };

    public int getIconHeight() {
    return 30;
    }

    public int getIconWidth() {
    return 30;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
    g.translate(x, y);
    g.setColor(Color.lightGray);
    g.fillRect(0, 0, getIconWidth(), getIconHeight());
    g.setColor(Color.darkGray);
    g.drawRect(0, 0, getIconWidth(), getIconHeight());
    g.translate(-x, -y);
    }
    }
    }