当前位置: 技术问答>java相关
此问题有点复杂,100份可能太少,不够再加,关于JTree的使用
来源: 互联网 发布时间:2015-09-06
本文导语: 我现在有棵JTree,共有三个节点,要求每个节点的图标不一样,而且每个图标在获得焦点时,与原来又不一样。同时,这三个节点又有两个子节点,这两个子节点图标也不一样,如何做?希望各位不要怕太麻烦,帮小弟...
我现在有棵JTree,共有三个节点,要求每个节点的图标不一样,而且每个图标在获得焦点时,与原来又不一样。同时,这三个节点又有两个子节点,这两个子节点图标也不一样,如何做?希望各位不要怕太麻烦,帮小弟一把,多谢!能给出能运行的源代码更好,解决就能得100分,两人解决再加100分
|
package tree;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
public class Mytree4 extends Applet {
boolean isStandalone = false;
JScrollPane scrollPane = new JScrollPane();
JTree jTree1 = null;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
/////////////////////////////////////////////////////////////
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//////////////////////////////////////////////////////////////
public Mytree4() {
}
/////////////////////////////////////////////////////////////
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//////////////////////////////////////////////////
private void jbInit() throws Exception {
jTextField1.setBounds(new Rectangle(195, 17, 148, 42));
jButton2.setFont(new java.awt.Font("Serif", 0, 12));
jButton2.setText("exit");
jButton2.setBounds(new Rectangle(308, 421, 69, 32));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton1.setFont(new java.awt.Font("Serif", 0, 12));
jButton1.setText("but");
jButton1.setBounds(new Rectangle(168, 421, 69, 32));
this.setBackground(Color.lightGray);
this.setLayout(null);
creattree();
/////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////
public String getAppletInfo() {
return "Applet Information";
}
/////////////////////////////////////////////////////////////
public String[][] getParameterInfo() {
return null;
}
public void creattree()
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的电脑");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夹");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");
///////////////////////////////////////
DefaultTreeModel treeModel = new DefaultTreeModel(root);
treeModel.insertNodeInto(node1, root, root.getChildCount());
treeModel.insertNodeInto(node2, root, root.getChildCount());
treeModel.insertNodeInto(node3, root, root.getChildCount());
treeModel.insertNodeInto(node4, root, root.getChildCount());
///////////////////////////////////////
DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("公司文件");
treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
leafnode = new DefaultMutableTreeNode("个人信件");
treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
leafnode = new DefaultMutableTreeNode("私人文件");
treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
///////////////////////////////////////
leafnode = new DefaultMutableTreeNode("本机磁盘(C:)");
treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
leafnode = new DefaultMutableTreeNode("本机磁盘(D:)");
treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
leafnode = new DefaultMutableTreeNode("本机磁盘(E:)");
treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
///////////////////////////////////////
DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("网站列表");
treeModel.insertNodeInto(node31, node3, node3.getChildCount());
leafnode = new DefaultMutableTreeNode("奇摩站");
treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
leafnode = new DefaultMutableTreeNode("职棒消息");
treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
leafnode = new DefaultMutableTreeNode("网络书店");
treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
///////////////////////////////////////
JTree tree = new JTree(treeModel);
tree.setRowHeight(20);
///////////////////////////////////////
DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
cellRenderer.setLeafIcon(new ImageIcon(".\icons\tt.gif"));
cellRenderer.setOpenIcon(new ImageIcon(".\icons\open24.gif"));
cellRenderer.setClosedIcon(new ImageIcon(".\icons\close24.gif"));
///////////////////////////////////////
cellRenderer.setFont(new Font("宋体",Font.PLAIN,12));
cellRenderer.setBackgroundNonSelectionColor(Color.white);
cellRenderer.setBackgroundSelectionColor(Color.yellow);
cellRenderer.setBorderSelectionColor(Color.red);
cellRenderer.setTextNonSelectionColor(Color.black);
cellRenderer.setTextSelectionColor(Color.blue);
///////////////////////////////////////
tree.setEditable(false);
JScrollPane scrollPane = new JScrollPane();
/////////////////////////////////////////////
tree.setBounds(new Rectangle(80, 5, 162, 321));
scrollPane.setBounds(new Rectangle(4, 3, 146, 368));
this.add(scrollPane);
this.add(jTextField1, null);
this.add(jButton2, null);
this.add(jButton1, null);
scrollPane.setViewportView(tree);
}
/////////////////////////////////////////////////////////////
public static void main(String[] args) {
Mytree4 applet = new Mytree4();
applet.isStandalone =true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("树实例");
frame.setResizable(false);
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(580,517);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
////////////////////////////////////////////
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
public class Mytree4 extends Applet {
boolean isStandalone = false;
JScrollPane scrollPane = new JScrollPane();
JTree jTree1 = null;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
/////////////////////////////////////////////////////////////
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//////////////////////////////////////////////////////////////
public Mytree4() {
}
/////////////////////////////////////////////////////////////
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//////////////////////////////////////////////////
private void jbInit() throws Exception {
jTextField1.setBounds(new Rectangle(195, 17, 148, 42));
jButton2.setFont(new java.awt.Font("Serif", 0, 12));
jButton2.setText("exit");
jButton2.setBounds(new Rectangle(308, 421, 69, 32));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton1.setFont(new java.awt.Font("Serif", 0, 12));
jButton1.setText("but");
jButton1.setBounds(new Rectangle(168, 421, 69, 32));
this.setBackground(Color.lightGray);
this.setLayout(null);
creattree();
/////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////
public String getAppletInfo() {
return "Applet Information";
}
/////////////////////////////////////////////////////////////
public String[][] getParameterInfo() {
return null;
}
public void creattree()
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的电脑");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夹");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");
///////////////////////////////////////
DefaultTreeModel treeModel = new DefaultTreeModel(root);
treeModel.insertNodeInto(node1, root, root.getChildCount());
treeModel.insertNodeInto(node2, root, root.getChildCount());
treeModel.insertNodeInto(node3, root, root.getChildCount());
treeModel.insertNodeInto(node4, root, root.getChildCount());
///////////////////////////////////////
DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("公司文件");
treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
leafnode = new DefaultMutableTreeNode("个人信件");
treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
leafnode = new DefaultMutableTreeNode("私人文件");
treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
///////////////////////////////////////
leafnode = new DefaultMutableTreeNode("本机磁盘(C:)");
treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
leafnode = new DefaultMutableTreeNode("本机磁盘(D:)");
treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
leafnode = new DefaultMutableTreeNode("本机磁盘(E:)");
treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
///////////////////////////////////////
DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("网站列表");
treeModel.insertNodeInto(node31, node3, node3.getChildCount());
leafnode = new DefaultMutableTreeNode("奇摩站");
treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
leafnode = new DefaultMutableTreeNode("职棒消息");
treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
leafnode = new DefaultMutableTreeNode("网络书店");
treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
///////////////////////////////////////
JTree tree = new JTree(treeModel);
tree.setRowHeight(20);
///////////////////////////////////////
DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
cellRenderer.setLeafIcon(new ImageIcon(".\icons\tt.gif"));
cellRenderer.setOpenIcon(new ImageIcon(".\icons\open24.gif"));
cellRenderer.setClosedIcon(new ImageIcon(".\icons\close24.gif"));
///////////////////////////////////////
cellRenderer.setFont(new Font("宋体",Font.PLAIN,12));
cellRenderer.setBackgroundNonSelectionColor(Color.white);
cellRenderer.setBackgroundSelectionColor(Color.yellow);
cellRenderer.setBorderSelectionColor(Color.red);
cellRenderer.setTextNonSelectionColor(Color.black);
cellRenderer.setTextSelectionColor(Color.blue);
///////////////////////////////////////
tree.setEditable(false);
JScrollPane scrollPane = new JScrollPane();
/////////////////////////////////////////////
tree.setBounds(new Rectangle(80, 5, 162, 321));
scrollPane.setBounds(new Rectangle(4, 3, 146, 368));
this.add(scrollPane);
this.add(jTextField1, null);
this.add(jButton2, null);
this.add(jButton1, null);
scrollPane.setViewportView(tree);
}
/////////////////////////////////////////////////////////////
public static void main(String[] args) {
Mytree4 applet = new Mytree4();
applet.isStandalone =true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("树实例");
frame.setResizable(false);
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(580,517);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
////////////////////////////////////////////
}
|
http://www.csdn.net/expert/topic/583/583148.xml?temp=.8619806