展开全部
点击不同节点,显示不同信息?你要显32313133353236313431303231363533e59b9ee7ad9431333332633038示哪些信息求具体啊 ,而且你的代码不是点击左边节点,右边不是已经显示你点击节点的名称。你要显示其他信息,起码先要把信息放到节点的userOject对象中吧,而且你的节点都是写死的啊,为啥不设置成从数据库动态生成树?
给你一个写死的列子 如何重写树吧 。不懂可以问我。
public class DBTree extends JPanel {
// 这几个设置节点图片。
static final ImageIcon DB_host = new ImageIcon("expandedfolder.gif");
static final ImageIcon DB = new ImageIcon("expandedfolder.gif");
static final ImageIcon table = new ImageIcon("expandedfolder.gif");
static final ImageIcon ICON_EXPANDEDFOLDER = new ImageIcon(
"expandedfolder.gif");
protected JTree m_tree;
protected DefaultTreeModel m_model;
public Map userMap = new HashMap();
public DBTree() {
// // 你树节点要显示的信息:
// String nodeMessage,
// // 树节点的名称
// String nodeName,
DefaultMutableTreeNode root = new DefaultMutableTreeNode(
// IconData 封装节点的userObject对象。
new IconData(ICON_EXPANDEDFOLDER, "好书中意哦的啊我的" ,"A1 马克思、恩格斯著作"));
// 将用户对象和其对应的信息保存到集合中
userMap.put(root.getUserObject(), "好书中意哦的啊我的");
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode(
// IconData 封装节点的userObject对象。
new IconData(ICON_EXPANDEDFOLDER, "累了你iikdwia","A2 列宁著作、"));
userMap.put(root2.getUserObject(), "累了你iikdwia");
root.add(root2);
//下面省略,。。。。。。
try {// 使得界面的钥匙变成系统默认的:
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e1) {
e1.printStackTrace();
}
m_model = new DefaultTreeModel(root);
m_tree = new JTree(m_model);
m_tree.putClientProperty("JTree.lineStyle", "Angled");
TreeCellRenderer renderer = new IconCellRenderer();
m_tree.setCellRenderer(renderer);
m_tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
m_tree.setShowsRootHandles(true);
m_tree.setEditable(false);
JScrollPane s = new JScrollPane();
setLayout(new BorderLayout());
s.getViewport().add(m_tree);
add(s, BorderLayout.CENTER);
m_tree.addMouseListener(new MouseListener() {
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
TreePath path = m_tree.getSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
.getLastPathComponent();
String nodeMessa = (String) userMap.get(node .getUserObject());
//这里点击左边节点,会将其中信息在控制台输出。
System.out.println(nodeMessa);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
DefaultMutableTreeNode getTreeNode(TreePath path) {
return (DefaultMutableTreeNode) (path.getLastPathComponent());
}
public void treeCollapsed(TreeExpansionEvent event) {
}
public static void main(String argv[]) {
JFrame frame = new JFrame("树");
JPanel panel=new DBTree();
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
}
}
class IconCellRenderer extends JLabel implements TreeCellRenderer {
protected Color m_textSelectionColor;
protected Color m_textNonSelectionColor;
protected Color m_bkSelectionColor;
protected Color m_bkNonSelectionColor;
protected Color m_borderSelectionColor;
protected boolean m_selected;
public IconCellRenderer() {
super();
m_textSelectionColor = UIManager.getColor("Tree.selectionForeground");
m_textNonSelectionColor = UIManager.getColor("Tree.textForeground");
m_bkSelectionColor = UIManager.getColor("Tree.selectionBackground");
m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground");
m_borderSelectionColor = UIManager
.getColor("Tree.selectionBorderColor");
setOpaque(false);
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object obj = node.getUserObject();
setText(obj.toString());
if (obj instanceof Boolean)
setText("Retrieving data...");
if (obj instanceof IconData) {
IconData idata = (IconData) obj;
setIcon(idata.getM_icon());
} else
setIcon(null);
setFont(tree.getFont());
setForeground(sel ? m_textSelectionColor : m_textNonSelectionColor);
setBackground(sel ? m_bkSelectionColor : m_bkNonSelectionColor);
m_selected = sel;
return this;
}
public void paintComponent(Graphics g) {
Color bColor = getBackground();
Icon icon = getIcon();
g.setColor(bColor);
int offset = 0;
if (icon != null && getText() != null)
offset = (icon.getIconWidth() + getIconTextGap());
g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
if (m_selected) {
g.setColor(m_borderSelectionColor);
g.drawRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
}
super.paintComponent(g);
}
}
// 封装节点信息的类
class IconData {
protected Icon m_icon;
protected Object m_nodeName;
protected Object m_nodMeassage;
public String toString() {
return m_nodMeassage.toString();
}
public Icon getM_icon() {
return m_icon;
}
public IconData(Icon m_icon, Object m_nodeName, Object m_nodMeassage) {
super();
this.m_icon = m_icon;
this.m_nodeName = m_nodeName;
this.m_nodMeassage = m_nodMeassage;
}
public void setM_icon(Icon m_icon) {
this.m_icon = m_icon;
}
public Object getM_nodeName() {
return m_nodeName;
}
public void setM_nodeName(Object m_nodeName) {
this.m_nodeName = m_nodeName;
}
public Object getM_nodMeassage() {
return m_nodMeassage;
}
public void setM_nodMeassage(Object m_nodMeassage) {
this.m_nodMeassage = m_nodMeassage;
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起