用户列表应该如何显示? 就是QQ群里面用户列表那种效果,即每个用户用图标和文字表示,点击鼠标左键可以选定一个用户。应该用哪种组件来做,试了好多都不行。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上的朋友能说的具体点吗?我是用java写聊天室,combox这个是下拉列表吧,它只能显示一个用户,好像不行的。 把你的代码发给我:[email protected] 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); } }} 关于动态代理Proxy.newProxyInstance方法参数的理解问题 请教大虾:java实现Comparable接口问题? 线程问题,如何控制每天固定时间启动线程? 如何判断异常(分不够可以再加) Tomcat5.0.28的servlet问题,请高手给我指点一下 请教:如何下载Wrox出版社书中的代码? 我是一个初学者,刚刚开始做聊天室程序,遇到一个问题怎么也解决不了,请大家帮忙看看 哪位大侠知道我的问题出在哪,在线等待 java调用unix shell问题 关于对象 一个既简单又很难实现的问题 MD5加密中文字符?
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);
}
}
}