一个好像从来没有人问过的有关JTree的问题。 to xmpp:我找了一下,没有Tree这个类。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该可以,只不过是幅图片吗,呵呵;)记的有个setUI什么的可以用吧,你去看看jTree的源码不就清楚了吗;)HOHO~~~~~~~~ JTree jt = new JTree();jt.setCellRenderer(new MyCellRenderer());//MyCellRendererimport java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;import javax.swing.plaf.ColorUIResource;public class MyCellRenderer extends JPanel implements TreeCellRenderer { //protected JCheckBox check; protected TreeLabel label; ImageIcon icon1 = new ImageIcon( PcedMainFrame.class.getResource("whitepoint.gif")); ImageIcon icon2 = new ImageIcon( PcedMainFrame.class.getResource("blackpoint.gif")); public MyCellRenderer() { setLayout(null); add(label = new TreeLabel()); 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()); label.setFont(tree.getFont()); label.setText(stringValue); label.setSelected(isSelected); label.setFocus(hasFocus); if(isSelected){ label.setIcon(icon2); }else{ label.setIcon(icon1); } return this; } public Dimension getPreferredSize() { Dimension d_label = label.getPreferredSize(); return new Dimension(d_label.width, d_label.height); } public void doLayout() { Dimension d_label = label.getPreferredSize(); int y_check = 0; int y_label = 0; y_label = (d_label.height)/2; label.setLocation(0,y_label); label.setBounds(0,0,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) { label.setForeground(Color.white); g.setColor(Color.BLUE); } else { label.setForeground(Color.black); 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; } }} to windloft:谢谢windloft的关注,但是还没解决该问题。因为该问题不是要更换节点左边的图标,而是要更换节点左边图标再左边的那个图标(呵呵,比较绕口)。比如在windows资源管理器中,闭合的文件夹图标左边还有一个“+”的图标;而打开的文件夹图标左边还有一个“-”的图标。现在问题是要在Metal风格中也显示这样的图标。 JTree的结构是这样的,由TreeUI负责你所需要的部分,而由TreeCellRenderer负责绘制每个节点,比如BasicTreeUI有这样一组APIprotected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定 JTree的结构是这样的,由TreeUI负责你所需要的部分,而由TreeCellRenderer负责绘制每个节点,比如BasicTreeUI有这样一组APIprotected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定 JTree的结构是这样的,由TreeUI负责你所需要的部分,而由TreeCellRenderer负责绘制每个节点,比如BasicTreeUI有这样一组APIprotected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定 怎么让WizardDialog能最小化到任务栏,而且不能使用最大化按钮 为什么jar 里边有这个META-INF文件夹 给想学JAVA的程序员介绍几本好书!(挑战JAVA程序员系列) java Applet缩放:当对网页进行拖拉缩放时,如果保证和GUI组件位置不变? java用什么编辑工具最好 再问,时间计算出错!那位帮着看看!还有50分给 怎么jb不能用来测试线程吗? 如何把JTable里面显示的内容的文字色,用红色表示? 有兴趣的来看看,谢谢 如何做半透明的JMenuItem,有人会吗? 关于异常处理中的finally块的问题·~ SCJD是什么东西,在线等待。
jt.setCellRenderer(new MyCellRenderer());//MyCellRenderer
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.plaf.ColorUIResource;public class MyCellRenderer extends JPanel implements TreeCellRenderer {
//protected JCheckBox check;
protected TreeLabel label;
ImageIcon icon1 = new ImageIcon(
PcedMainFrame.class.getResource("whitepoint.gif"));
ImageIcon icon2 = new ImageIcon(
PcedMainFrame.class.getResource("blackpoint.gif")); public MyCellRenderer() {
setLayout(null);
add(label = new TreeLabel());
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());
label.setFont(tree.getFont());
label.setText(stringValue);
label.setSelected(isSelected);
label.setFocus(hasFocus);
if(isSelected){
label.setIcon(icon2);
}else{
label.setIcon(icon1);
}
return this;
} public Dimension getPreferredSize() {
Dimension d_label = label.getPreferredSize();
return new Dimension(d_label.width, d_label.height);
} public void doLayout() {
Dimension d_label = label.getPreferredSize();
int y_check = 0;
int y_label = 0;
y_label = (d_label.height)/2;
label.setLocation(0,y_label);
label.setBounds(0,0,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) {
label.setForeground(Color.white);
g.setColor(Color.BLUE);
} else {
label.setForeground(Color.black);
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;
}
}
}
谢谢windloft的关注,但是还没解决该问题。
因为该问题不是要更换节点左边的图标,而是要更换节点左边图标再左边的那个图标(呵呵,比较绕口)。
比如在windows资源管理器中,闭合的文件夹图标左边还有一个“+”的图标;
而打开的文件夹图标左边还有一个“-”的图标。
现在问题是要在Metal风格中也显示这样的图标。
protected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right)
protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom)
protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定
protected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right)
protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom)
protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定
protected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right)
protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)
protected void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom)
protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定