java swing左边树图,当点击任意节点时,右边显示显示数据, 求一个简单的源码例子
展开全部
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class TestSwingTree extends JFrame {
private JPanel p;
public TestSwingTree(String title){
super(title);
}
public void init(){
Container c = this.getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("child1");
DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("child11");
DefaultMutableTreeNode child12 = new DefaultMutableTreeNode("child12");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("child2");
DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("child3");
DefaultMutableTreeNode child31 = new DefaultMutableTreeNode("child31");
root.add(child1);
root.add(child2);
root.add(child3);
child1.add(child11);
child1.add(child12);
child3.add(child31);
JTree tree = new JTree(root);
tree.setPreferredSize(new Dimension(120, 400));
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
p.removeAll();
JLabel l = new JLabel(e.getPath().toString());
l.setBounds(5, 190, 170, 20);
p.add(l);
p.repaint();
}
});
c.add(tree,BorderLayout.WEST);
p = new JPanel();
p.setLayout(null);
p.setPreferredSize(new Dimension(180, 400));
c.add(p,BorderLayout.CENTER);
this.setLocation(400, 300);
this.setSize(300, 400);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestSwingTree("Test Swing Jtree").init();
}
}
追问
哇,大神,我琢磨1天了,也没弄懂头绪, 如果点击左面的节点,右面显示的是另一个页面比如是student.java 里的数据,而且数据是从oracle表里查出来的, 怎么破? 大神,我愿意付费,帮我整个吧,还行
追答
没看懂,啥意思?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询