高级UI组件之树(JTree)

本文展示了一个使用Java Swing创建的简单树结构示例。该示例通过`DefaultMutableTreeNode`构建了一个包含学校管理层级的树形结构,并演示了如何在选中节点时更新文本区域的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  简略的说就是节点(DefaultMutableTreeNodes)加内容,内容再加内容,(前内容)便子节点。


package test;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.GridLayout;
import javax.swing.JTextArea;

public class NewTest {

	private DefaultMutableTreeNode root = new DefaultMutableTreeNode("学校管理");
	private DefaultMutableTreeNode t = new DefaultMutableTreeNode("教师");
	private DefaultMutableTreeNode s = new DefaultMutableTreeNode("学生");
	
	private DefaultMutableTreeNode t1 = new DefaultMutableTreeNode("孔子");
	private DefaultMutableTreeNode t2 = new DefaultMutableTreeNode("孟子");
	private DefaultMutableTreeNode s1 = new DefaultMutableTreeNode("子路");
	private DefaultMutableTreeNode s2 = new DefaultMutableTreeNode("子游");
	private DefaultMutableTreeNode s3 = new DefaultMutableTreeNode("子渊");
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					NewTest window = new NewTest();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public NewTest() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 543, 405);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(0, 0, 180, 358);
		panel.add(panel_1);
		panel_1.setLayout(null);
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(179, 0, 346, 358);
		panel.add(panel_2);
		panel_2.setLayout(new GridLayout(1, 0, 0, 0));
		
		JTextArea textArea = new JTextArea();
		panel_2.add(textArea);
		JTree tree = new JTree(root);
		root.add(s);
		root.add(t);
		s.add(s1);
		s.add(s2);
		s.add(s3);
		t.add(t1);
		t.add(t2);
		tree.setBounds(0, 0, 180, 358);
		panel_1.add(tree);
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				// TODO Auto-generated method stub
			 	DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
			    if(node.isLeaf()) {
			    	textArea.setText(node.getUserObject().toString());
			    }
			}
		});
		
		
		
		
	}
}

效果显示:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长臂人猿

客官们众筹请博主喝杯奶茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值