如何在jlist中加入面板? 想做一个类似QQ的界面程序,里面的好友列表打算用jlist布置,但是panel加入jlist以后总是返回面板的字符串,如何显示出原来的面板啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请问gongfuliang可否说得详细一点? public class CheckListExample extends JFrame { public CheckListExample() { super("CheckList Example"); String[] strs = { "swing", "home", "basic", "metal", "JList" }; final JList list = new JList(createData(strs)); Icon icon = MetalIconFactory.getFileChooserHomeFolderIcon(); ((CheckableItem) list.getModel().getElementAt(1)).setIcon(icon); list.setCellRenderer(new CheckListRenderer()); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setBorder(new EmptyBorder(0, 4, 0, 0)); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int index = list.locationToIndex(e.getPoint()); CheckableItem item = (CheckableItem) list.getModel() .getElementAt(index); item.setSelected(!item.isSelected()); Rectangle rect = list.getCellBounds(index, index); list.repaint(rect); } }); JScrollPane sp = new JScrollPane(list); final JTextArea textArea = new JTextArea(3, 10); JScrollPane textPanel = new JScrollPane(textArea); JButton printButton = new JButton("print"); printButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListModel model = list.getModel(); int n = model.getSize(); for (int i = 0; i < n; i++) { CheckableItem item = (CheckableItem) model.getElementAt(i); if (item.isSelected()) { textArea.append(item.toString()); textArea.append(System.getProperty("line.separator")); } } } }); JButton clearButton = new JButton("clear"); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.setText(""); } }); JPanel panel = new JPanel(new GridLayout(2, 1)); panel.add(printButton); panel.add(clearButton); getContentPane().add(sp, BorderLayout.CENTER); getContentPane().add(panel, BorderLayout.EAST); getContentPane().add(textPanel, BorderLayout.SOUTH); } private CheckableItem[] createData(String[] strs) { int n = strs.length; CheckableItem[] items = new CheckableItem[n]; for (int i = 0; i < n; i++) { items[i] = new CheckableItem(strs[i]); } return items; } class CheckableItem { private String str; private boolean isSelected; private Icon icon; public CheckableItem(String str) { this.str = str; isSelected = false; } public void setSelected(boolean b) { isSelected = b; } public boolean isSelected() { return isSelected; } public String toString() { return str; } public void setIcon(Icon icon) { this.icon = icon; } public Icon getIcon() { return icon; } } class CheckListRenderer extends CheckRenderer implements ListCellRenderer { Icon commonIcon; public CheckListRenderer() { check.setBackground(UIManager.getColor("List.textBackground")); label.setForeground(UIManager.getColor("List.textForeground")); commonIcon = UIManager.getIcon("Tree.leafIcon"); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) { setEnabled(list.isEnabled()); check.setSelected(((CheckableItem) value).isSelected()); label.setFont(list.getFont()); label.setText(value.toString()); label.setSelected(isSelected); label.setFocus(hasFocus); Icon icon = ((CheckableItem) value).getIcon(); if (icon == null) { icon = commonIcon; } label.setIcon(icon); return this; } } public static void main(String args[]) { CheckListExample frame = new CheckListExample(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setSize(300, 200); frame.setVisible(true); }} 这里Renderer就以一个Panel实现package JList.JListExamples1.CheckList;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import javax.swing.Icon;import javax.swing.JCheckBox;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTree;import javax.swing.UIManager;import javax.swing.plaf.ColorUIResource;import javax.swing.tree.TreeCellRenderer;import JTree.JTreeExamples2.CheckNode.CheckNode;/** * @version 1.1 04/24/99 */public class CheckRenderer extends JPanel implements TreeCellRenderer { protected JCheckBox check; protected TreeLabel label; public CheckRenderer() { setLayout(null); add(check = new JCheckBox()); add(label = new TreeLabel()); check.setBackground(UIManager.getColor("Tree.textBackground")); label.setForeground(UIManager.getColor("Tree.textForeground")); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) { String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, hasFocus); setEnabled(tree.isEnabled()); check.setSelected(((CheckNode) value).isSelected()); label.setFont(tree.getFont()); label.setText(stringValue); label.setSelected(isSelected); label.setFocus(hasFocus); if (leaf) { label.setIcon(UIManager.getIcon("Tree.leafIcon")); } else if (expanded) { label.setIcon(UIManager.getIcon("Tree.openIcon")); } else { label.setIcon(UIManager.getIcon("Tree.closedIcon")); } return this; } public Dimension getPreferredSize() { Dimension d_check = check.getPreferredSize(); Dimension d_label = label.getPreferredSize(); return new Dimension(d_check.width + d_label.width, (d_check.height < d_label.height ? d_label.height : d_check.height)); } public void doLayout() { Dimension d_check = check.getPreferredSize(); Dimension d_label = label.getPreferredSize(); int y_check = 0; int y_label = 0; if (d_check.height < d_label.height) { y_check = (d_label.height - d_check.height) / 2; } else { y_label = (d_check.height - d_label.height) / 2; } check.setLocation(0, y_check); check.setBounds(0, y_check, d_check.width, d_check.height); label.setLocation(d_check.width, y_label); label.setBounds(d_check.width, y_label, d_label.width, d_label.height); } public void setBackground(Color color) { if (color instanceof ColorUIResource) color = null; super.setBackground(color); } public class TreeLabel extends JLabel { boolean isSelected; boolean hasFocus; public TreeLabel() { } public void setBackground(Color color) { if (color instanceof ColorUIResource) color = null; super.setBackground(color); } public void paint(Graphics g) { String str; if ((str = getText()) != null) { if (0 < str.length()) { if (isSelected) { g.setColor(UIManager .getColor("Tree.selectionBackground")); } else { g.setColor(UIManager.getColor("Tree.textBackground")); } Dimension d = getPreferredSize(); int imageOffset = 0; Icon currentI = getIcon(); if (currentI != null) { imageOffset = currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1); } g.fillRect(imageOffset, 0, d.width - 1 - imageOffset, d.height); if (hasFocus) { g.setColor(UIManager .getColor("Tree.selectionBorderColor")); g.drawRect(imageOffset, 0, d.width - 1 - imageOffset, d.height - 1); } } } super.paint(g); } public Dimension getPreferredSize() { Dimension retDimension = super.getPreferredSize(); if (retDimension != null) { retDimension = new Dimension(retDimension.width + 3, retDimension.height); } return retDimension; } public void setSelected(boolean isSelected) { this.isSelected = isSelected; } public void setFocus(boolean hasFocus) { this.hasFocus = hasFocus; } }} 我还不是很明白,可否说明一下接口ListCellRenderer的作用是什么,里面应该放的东西是用来做什么的 ListCellRenderer是渲染器的接口,里面的getListCellRendererComponent用来定义你的组件的界面展现 有一个好严重的问题:import java.awt.*;import javax.swing.*;public class Test extends JFrame{ public Test(){ int number = 10; JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); JLabel[] label = new JLabel[number]; for(int i = 0; i < number; i++){ if(i % 2 == 0){ label[i] = new JLabel("123"); }else{ label[i] = new JLabel("你好吗?"); } } JList list = new JList(label); list.setCellRenderer(new MyCellRenderer()); JScrollPane scrollPane = new JScrollPane(list); mainPanel.add(scrollPane,BorderLayout.CENTER); this.setPreferredSize(new Dimension(350,300)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(mainPanel); pack(); setVisible(true); } public static void main(String[] args){ new Test(); } private class MyCellRenderer extends JPanel implements ListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setLayout(new BorderLayout()); add((JLabel)value,BorderLayout.CENTER); return this; } } }为什么label的字会合在一起的? spring(2.5)和struts2(2.0.14)集成问题!跪求高手指教! java小游戏 急!!!!!!问题问的已经是第三遍了,请高手赐教!谢绝非成心解决问题的!谢谢! 怎样用ant将hibernate用GB2312编译? 正则表达式的入门文档,关注者有分 关于程序里使用jdbc的问题 scj的310-025和sl-275考试有什么不一样? 谁有COM.ibm.db2.jdbc.app.DB2Drive驱动 一个简单的java问题,高分求助!!! jdbc中连接池的实现一定要自己写吗?有没现成的类? 问什么会输出0,0呢? 如何抓取网页内容的连接
super("CheckList Example");
String[] strs = { "swing", "home", "basic", "metal", "JList" }; final JList list = new JList(createData(strs)); Icon icon = MetalIconFactory.getFileChooserHomeFolderIcon();
((CheckableItem) list.getModel().getElementAt(1)).setIcon(icon); list.setCellRenderer(new CheckListRenderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(new EmptyBorder(0, 4, 0, 0));
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
CheckableItem item = (CheckableItem) list.getModel()
.getElementAt(index);
item.setSelected(!item.isSelected());
Rectangle rect = list.getCellBounds(index, index);
list.repaint(rect);
}
});
JScrollPane sp = new JScrollPane(list); final JTextArea textArea = new JTextArea(3, 10);
JScrollPane textPanel = new JScrollPane(textArea);
JButton printButton = new JButton("print");
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ListModel model = list.getModel();
int n = model.getSize();
for (int i = 0; i < n; i++) {
CheckableItem item = (CheckableItem) model.getElementAt(i);
if (item.isSelected()) {
textArea.append(item.toString());
textArea.append(System.getProperty("line.separator"));
}
}
}
});
JButton clearButton = new JButton("clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.add(printButton);
panel.add(clearButton); getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.EAST);
getContentPane().add(textPanel, BorderLayout.SOUTH);
} private CheckableItem[] createData(String[] strs) {
int n = strs.length;
CheckableItem[] items = new CheckableItem[n];
for (int i = 0; i < n; i++) {
items[i] = new CheckableItem(strs[i]);
}
return items;
} class CheckableItem {
private String str;
private boolean isSelected;
private Icon icon; public CheckableItem(String str) {
this.str = str;
isSelected = false;
} public void setSelected(boolean b) {
isSelected = b;
} public boolean isSelected() {
return isSelected;
} public String toString() {
return str;
} public void setIcon(Icon icon) {
this.icon = icon;
} public Icon getIcon() {
return icon;
}
} class CheckListRenderer extends CheckRenderer implements ListCellRenderer {
Icon commonIcon; public CheckListRenderer() {
check.setBackground(UIManager.getColor("List.textBackground"));
label.setForeground(UIManager.getColor("List.textForeground"));
commonIcon = UIManager.getIcon("Tree.leafIcon");
} public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled());
check.setSelected(((CheckableItem) value).isSelected());
label.setFont(list.getFont());
label.setText(value.toString());
label.setSelected(isSelected);
label.setFocus(hasFocus);
Icon icon = ((CheckableItem) value).getIcon();
if (icon == null) {
icon = commonIcon;
}
label.setIcon(icon);
return this;
}
} public static void main(String args[]) {
CheckListExample frame = new CheckListExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.tree.TreeCellRenderer;import JTree.JTreeExamples2.CheckNode.CheckNode;/**
* @version 1.1 04/24/99
*/
public class CheckRenderer extends JPanel implements TreeCellRenderer {
protected JCheckBox check;
protected TreeLabel label; public CheckRenderer() {
setLayout(null);
add(check = new JCheckBox());
add(label = new TreeLabel());
check.setBackground(UIManager.getColor("Tree.textBackground"));
label.setForeground(UIManager.getColor("Tree.textForeground"));
} public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
String stringValue = tree.convertValueToText(value, isSelected,
expanded, leaf, row, hasFocus);
setEnabled(tree.isEnabled());
check.setSelected(((CheckNode) value).isSelected());
label.setFont(tree.getFont());
label.setText(stringValue);
label.setSelected(isSelected);
label.setFocus(hasFocus);
if (leaf) {
label.setIcon(UIManager.getIcon("Tree.leafIcon"));
} else if (expanded) {
label.setIcon(UIManager.getIcon("Tree.openIcon"));
} else {
label.setIcon(UIManager.getIcon("Tree.closedIcon"));
}
return this;
} public Dimension getPreferredSize() {
Dimension d_check = check.getPreferredSize();
Dimension d_label = label.getPreferredSize();
return new Dimension(d_check.width + d_label.width,
(d_check.height < d_label.height ? d_label.height
: d_check.height));
} public void doLayout() {
Dimension d_check = check.getPreferredSize();
Dimension d_label = label.getPreferredSize();
int y_check = 0;
int y_label = 0;
if (d_check.height < d_label.height) {
y_check = (d_label.height - d_check.height) / 2;
} else {
y_label = (d_check.height - d_label.height) / 2;
}
check.setLocation(0, y_check);
check.setBounds(0, y_check, d_check.width, d_check.height);
label.setLocation(d_check.width, y_label);
label.setBounds(d_check.width, y_label, d_label.width, d_label.height);
} public void setBackground(Color color) {
if (color instanceof ColorUIResource)
color = null;
super.setBackground(color);
} public class TreeLabel extends JLabel {
boolean isSelected;
boolean hasFocus; public TreeLabel() {
} public void setBackground(Color color) {
if (color instanceof ColorUIResource)
color = null;
super.setBackground(color);
} public void paint(Graphics g) {
String str;
if ((str = getText()) != null) {
if (0 < str.length()) {
if (isSelected) {
g.setColor(UIManager
.getColor("Tree.selectionBackground"));
} else {
g.setColor(UIManager.getColor("Tree.textBackground"));
}
Dimension d = getPreferredSize();
int imageOffset = 0;
Icon currentI = getIcon();
if (currentI != null) {
imageOffset = currentI.getIconWidth()
+ Math.max(0, getIconTextGap() - 1);
}
g.fillRect(imageOffset, 0, d.width - 1 - imageOffset,
d.height);
if (hasFocus) {
g.setColor(UIManager
.getColor("Tree.selectionBorderColor"));
g.drawRect(imageOffset, 0, d.width - 1 - imageOffset,
d.height - 1);
}
}
}
super.paint(g);
} public Dimension getPreferredSize() {
Dimension retDimension = super.getPreferredSize();
if (retDimension != null) {
retDimension = new Dimension(retDimension.width + 3,
retDimension.height);
}
return retDimension;
} public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
} public void setFocus(boolean hasFocus) {
this.hasFocus = hasFocus;
}
}
}
import javax.swing.*;public class Test extends JFrame{
public Test(){
int number = 10;
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JLabel[] label = new JLabel[number];
for(int i = 0; i < number; i++){
if(i % 2 == 0){
label[i] = new JLabel("123");
}else{
label[i] = new JLabel("你好吗?");
}
}
JList list = new JList(label);
list.setCellRenderer(new MyCellRenderer());
JScrollPane scrollPane = new JScrollPane(list);
mainPanel.add(scrollPane,BorderLayout.CENTER);
this.setPreferredSize(new Dimension(350,300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(mainPanel);
pack();
setVisible(true);
}
public static void main(String[] args){
new Test();
}
private class MyCellRenderer extends JPanel implements ListCellRenderer {
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setLayout(new BorderLayout());
add((JLabel)value,BorderLayout.CENTER);
return this;
}
}
}为什么label的字会合在一起的?