用户列表应该如何显示? 就是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); } }} 最近忙着找工作,今天终于有时间了,继续贴我的学习笔记,java集合框架中的工具类 发了聊天记录,引出大家对java中优化现象的了解。 spring mvc框架 请写出下面代码的输出结果 关于java swing 中的validate()方法 怎样实现弹出一个已经做好的界面? 内部错误: 数据存取的索引无效 那个控件里能绘制图形?如何能将绘制的图形转换成GIF或者JPEG格式的文件保存? 请教 几个问题!!!!谢谢!!! Java Applet小程序怎样向html传参数? 一个既简单又很难实现的问题 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);
}
}
}