我编写了一个JTree,含有显示框,帮我看一下为什么框不显示内容阿。
package tree2;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTree;
import java.awt.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame1 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextnodename = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField jTexttypename = new JTextField();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JTextField jTextnodetype = new JTextField();
JTextField jTextFieldnode = new JTextField();
JTree jTree1;
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(450, 350));
setTitle("欢迎使用 JTree");
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点");
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("书籍");
contentPane.setMaximumSize(new Dimension(600, 400));
contentPane.setPreferredSize(new Dimension(600, 400));
root.add(parent);
DefaultMutableTreeNode java = new DefaultMutableTreeNode("Java");
parent.add(java);
DefaultMutableTreeNode complete = new DefaultMutableTreeNode("顽症参考资料");
java.add(complete);
DefaultMutableTreeNode advanced = new DefaultMutableTreeNode("高级Java编程");
java.add(advanced);
DefaultMutableTreeNode oracle = new DefaultMutableTreeNode("学习 ORacle");
advanced.add(oracle);
DefaultMutableTreeNode learn = new DefaultMutableTreeNode("学习 Sql");
oracle.add(learn);
DefaultMutableTreeNode sql = new DefaultMutableTreeNode("学习编程");
oracle.add(sql);
DefaultMutableTreeNode program = new DefaultMutableTreeNode("Jsp");
oracle.add(program);
DefaultMutableTreeNode jsp = new DefaultMutableTreeNode("JSP");
parent.add(jsp);
DefaultMutableTreeNode jsp1 = new DefaultMutableTreeNode("学习JSP");
jsp.add(jsp1);
DefaultMutableTreeNode jsp2 = new DefaultMutableTreeNode("使用Jsp编程");
jsp.add(jsp2);
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("C#");
parent.add(leaf);
DefaultMutableTreeNode programing = new DefaultMutableTreeNode("使用C#编程");
leaf.add(programing);
parent = new DefaultMutableTreeNode("软件");
root.add(parent);
leaf = new DefaultMutableTreeNode("操作系统");
parent.add(leaf);
DefaultMutableTreeNode dosobj = new DefaultMutableTreeNode(
"Windows2000");
leaf.add(dosobj);
DefaultMutableTreeNode windowsobj = new DefaultMutableTreeNode(
"WindowsServer2000");
leaf.add(windowsobj);
DefaultMutableTreeNode winobj = new DefaultMutableTreeNode(
"Windows2000Professinal");
leaf.add(winobj);
leaf = new DefaultMutableTreeNode("数据库");
parent.add(leaf);
DefaultMutableTreeNode accessobj = new DefaultMutableTreeNode(
"MS-Access");
leaf.add(accessobj);
DefaultMutableTreeNode mssqlobj = new DefaultMutableTreeNode(
"MS-Sql Server");
leaf.add(mssqlobj);
JTree jTree1 = new JTree(root);
jTree1.setBounds(new Rectangle(12, 28, 183, 305));
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jLabel1.setText("父节点名称:");
jLabel1.setBounds(new Rectangle(203, 58, 84, 28));
jTextnodename.setBorder(BorderFactory.createLoweredBevelBorder());
jTextnodename.setBounds(new Rectangle(271, 68, 156, 27));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jLabel2.setText("节点名称:");
jLabel2.setBounds(new Rectangle(203, 116, 74, 29));
jTexttypename.setBorder(BorderFactory.createLoweredBevelBorder());
jTexttypename.setBounds(new Rectangle(265, 118, 157, 26));
jLabel3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jLabel3.setText("节点类型:");
jLabel3.setBounds(new Rectangle(204, 171, 76, 34));
jLabel4.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jLabel4.setText("子节点数:");
jLabel4.setBounds(new Rectangle(206, 222, 82, 34));
jTextnodetype.setBorder(BorderFactory.createLoweredBevelBorder());
jTextnodetype.setBounds(new Rectangle(266, 182, 159, 20));
jTextFieldnode.setBorder(BorderFactory.createLoweredBevelBorder());
jTextFieldnode.setBounds(new Rectangle(272, 236, 146, 23));
contentPane.add(jLabel1);
contentPane.add(jLabel2);
contentPane.add(jTextnodename);
contentPane.add(jTexttypename);
contentPane.add(jLabel3);
contentPane.add(jTextnodetype);
contentPane.add(jLabel4);
contentPane.add(jTextFieldnode);
contentPane.add(jTree1);
}
public void jTree1_valueChanged(TreeSelectionEvent e) {
try{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)jTree1.
getLastSelectedPathComponent();
String name="";
Object nodeInfo=node.getUserObject();//取节点名称
if(node.isRoot())
{
jTextnodename.setText("");
jTextFieldnode.setText(nodeInfo.toString());
jTexttypename.setText("根节点");
jTextnodetype.setText(""+node.getChildCount());
}
else {
name=node.getParent().toString();
jTextnodename.setText(name);
jTextFieldnode.setText(nodeInfo.toString());
if (node.isLeaf())
{
jTexttypename.setText("叶节点");
}else {
jTexttypename.setText("支节点");
}
int number =node.getChildCount();
jTextFieldnode.setText(""+number);
}
} catch (Exception ex)
{}
}
}
class Frame1_jTree1_treeSelectionAdapter implements TreeSelectionListener {
private Frame1 adaptee;
Frame1_jTree1_treeSelectionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void valueChanged(TreeSelectionEvent e) {
adaptee.jTree1_valueChanged(e);
}
}