Java swing中树状组件JTree的使用,左边点击不同菜单,右边显示不同页面

本文主要是介绍Java swing中树状组件JTree的使用,左边点击不同菜单,右边显示不同页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果:单击左边树状菜单,右边显示不同的页面

文章目录

    • 效果图
    • 代码

效果图

在这里插入图片描述
在这里插入图片描述

代码

我尽量删除了一些不需要的代码,包括数据处理部分的代码,只留下了页面的代码。
实现右边页面随左边树状菜单的变化这部分代码并不是很复杂,这部分页面代码我提取出来了,有问题请留言

package Wu;import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.ScrollPane;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;public class ManagerUser extends JFrame implements TreeSelectionListener{JTree tree;private JPanel p;ManagerUser ManagerUser=this;ManagerUser(){Container c = this.getContentPane();DefaultMutableTreeNode root=new DefaultMutableTreeNode("分类");DefaultMutableTreeNode route=new DefaultMutableTreeNode("路线管理");DefaultMutableTreeNode people=new DefaultMutableTreeNode("用户管理");DefaultMutableTreeNode add=new DefaultMutableTreeNode("添加路线");DefaultMutableTreeNode revice=new DefaultMutableTreeNode("修改路线信息");DefaultMutableTreeNode delete=new DefaultMutableTreeNode("删除路线");DefaultMutableTreeNode See=new DefaultMutableTreeNode("查看所有路线");DefaultMutableTreeNode show=new DefaultMutableTreeNode("显示用户信息");DefaultMutableTreeNode chat=new DefaultMutableTreeNode("用户咨询界面");root.add(route);root.add(people);route.add(add);route.add(revice);route.add(delete);route.add(See);people.add(show);people.add(chat);tree=new JTree(root);tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);tree.addTreeSelectionListener(this);p=new JPanel();p.setSize(1000,1000);c.add(tree,BorderLayout.WEST);c.add(p,BorderLayout.CENTER);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);//validate();}public void valueChanged(TreeSelectionEvent e) {DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();if(selectionNode.isLeaf()){ //叶子节点的监听System.out.println(selectionNode.toString());if(selectionNode.toString()=="添加路线") {    //添加路线AddRouteListener computer = new AddRouteListener();JPanel p1=new JPanel();System.out.println(selectionNode.toString());JTextField textInput1;JTextField textInput2;JTextField textInput3;JTextField textInput4;JTextField textInput5;JTextField textInput6;JTextField textInput7;JTextField textInput8;JTextField textInput9;JTextField textInput10;JButton button1,button2,button3;Box box,box1,box2,box3,box4,box5;box= Box.createVerticalBox();box1 = Box.createHorizontalBox();box4 = Box.createHorizontalBox();box2 = Box.createVerticalBox();//垂直的盒子box3 = Box.createVerticalBox();box2.add(new JLabel("要增加的班次:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("起点站:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("终点站:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("软卧票数:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("发车时间:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("行车时间:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("统一价格:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("硬卧票数:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("硬座票数:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("无座票数:"));button1 = new JButton("确定");button2 = new JButton("退出");button3 = new JButton("刷新");textInput1 = new JTextField(10);box3.add(textInput1);box3.add(Box.createHorizontalStrut(1));textInput2 = new JTextField(10);box3.add(textInput2); box3.add(Box.createHorizontalStrut(1));textInput3 = new JTextField(10);box3.add(textInput3);box3.add(Box.createHorizontalStrut(1));textInput4 = new JTextField(10);box3.add(textInput4);textInput5 = new JTextField(10);box3.add(textInput5);textInput6 = new JTextField(10);box3.add(textInput6); textInput7 = new JTextField(10);box3.add(textInput7);textInput8 = new JTextField(10);box3.add(textInput8);textInput9 = new JTextField(10);box3.add(textInput9);textInput10 = new JTextField(10);box3.add(textInput10);box4.add(button1);box4.add(Box.createHorizontalStrut(20));box4.add(button2);box4.add(button3);box1.add(box2);box1.add(box3);box.add(box1);box.add(box4);p.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if (e.getButton() == MouseEvent.BUTTON3) {// 弹出菜单jp.show(p, e.getX(), e.getY());}}/* void copy_mousePressed(ActionEvent e) {String str=e.getActionCommand();if(str.equals(copy.getText())) {this.copy();}else if(str.equals(copy.getText())) {this.paste();}}*/});p.setVisible(true);p1.add(box);p1.setSize(800,800);//p1.setBounds(0, 0, 200, 100);p.removeAll();p.add(p1);}if(selectionNode.toString()=="修改路线信息") {    //修改路线ReviseRouteListener computer = new ReviseRouteListener();JPanel p1=new JPanel();System.out.println(selectionNode.toString());JTextField textInput1;JTextField textInput2;JTextField textInput3;JTextField textInput4;JTextField textInput5;JTextField textInput6;JTextField textInput7;JTextField textInput8;JTextField textInput9;JTextField textInput10;JButton button1,button2,button3;Box box,box1,box2,box3,box4,box5;box= Box.createVerticalBox();box1 = Box.createHorizontalBox();box4 = Box.createHorizontalBox();box2 = Box.createVerticalBox();//垂直的盒子box3 = Box.createVerticalBox();box2.add(new JLabel("要修改的班次:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("起点站:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("终点站:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("软卧票数:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("发车时间:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("行车时间:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("统一价格:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("硬卧票数:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("硬座票数:"));box2.add(Box.createHorizontalStrut(1));box2.add(new JLabel("无座票数:"));button1 = new JButton("确定");button2 = new JButton("退出");button3 = new JButton("刷新");textInput1 = new JTextField(10);box3.add(textInput1);box3.add(Box.createHorizontalStrut(1));textInput2 = new JTextField(10);box3.add(textInput2); box3.add(Box.createHorizontalStrut(1));textInput3 = new JTextField(10);box3.add(textInput3);box3.add(Box.createHorizontalStrut(1));textInput4 = new JTextField(10);box3.add(textInput4);textInput5 = new JTextField(10);box3.add(textInput5);textInput6 = new JTextField(10);box3.add(textInput6); textInput7 = new JTextField(10);box3.add(textInput7);textInput8 = new JTextField(10);box3.add(textInput8);textInput9 = new JTextField(10);box3.add(textInput9);textInput10 = new JTextField(10);box3.add(textInput10);box4.add(button1);box4.add(Box.createHorizontalStrut(20));box4.add(button2);box4.add(button3);box1.add(box2);box1.add(box3);box.add(box1);box.add(box4);p1.add(box);p1.setBounds(0, 0, 800, 800);p.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if (e.getButton() == MouseEvent.BUTTON3) {// 弹出菜单jp.show(p, e.getX(), e.getY());}}});p.setVisible(true);p.removeAll();p.add(p1);}if(selectionNode.toString()=="删除路线") {    //删除路线DeleteRouteListener computer = new DeleteRouteListener();JPanel p1=new JPanel();System.out.println(selectionNode.toString());JTextField textInput1;JTextField textInput2;JTextField textInput3;JTextField textInput4;JTextField textInput5;JTextField textInput6;JButton button1,button2,button3;Box box,box1,box2,box3,box4,box5;box= Box.createVerticalBox();box1 = Box.createHorizontalBox();box4 = Box.createHorizontalBox();box2 = Box.createVerticalBox();//垂直的盒子box3 = Box.createVerticalBox();box2.add(new JLabel("要删除的班次:"));button1 = new JButton("确定");button2 = new JButton("退出");button3 = new JButton("刷新");textInput1 = new JTextField(10);box3.add(textInput1);box4.add(button1);box4.add(Box.createHorizontalStrut(20));box4.add(button2);box4.add(button3);box1.add(box2);box1.add(box3);box.add(box1);box.add(box4);p1.add(box);p1.setBounds(0, 0, 800, 800);p.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if (e.getButton() == MouseEvent.BUTTON3) {// 弹出菜单jp.show(p, e.getX(), e.getY());}}});p.setVisible(true);p.removeAll();p.add(p1);}if(selectionNode.toString()=="查看所有路线") {JButton button1=new JButton("查看所有路线");JPanel p1=new JPanel();p1.setLayout(new java.awt.BorderLayout());JTable table; Object a[][];a=new Object[1000][11];Object name[]= {"班次","起点站","终点站","发车时间","行车时间","统一价格","软卧剩余票数","硬卧剩余票数","硬座剩余票数","无座剩余票数","状态"};table=new JTable(a,name);table.setSize(1000,1000);//滚动条JScrollPane jsp=new JScrollPane(table);p1.add(jsp,BorderLayout.SOUTH);//p1.add(jsp,BorderLayout.CENTER);//a[45][3]="吴洪伟";for(int i=0;i<11;i++) {a[0][i]=name[i];}jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//水平滚动条始终显示jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//垂直滚动条始终显示p1.add(button1, BorderLayout.NORTH);table.getColumnModel().getColumn(0).setPreferredWidth(50);//设置表格每列的宽度table.getColumnModel().getColumn(1).setPreferredWidth(80);table.getColumnModel().getColumn(2).setPreferredWidth(80);table.getColumnModel().getColumn(3).setPreferredWidth(80);table.getColumnModel().getColumn(4).setPreferredWidth(80);table.getColumnModel().getColumn(5).setPreferredWidth(80);table.getColumnModel().getColumn(6).setPreferredWidth(80);table.getColumnModel().getColumn(7).setPreferredWidth(80);table.getColumnModel().getColumn(8).setPreferredWidth(80);table.getColumnModel().getColumn(9).setPreferredWidth(80);table.getColumnModel().getColumn(10).setPreferredWidth(80);//p1.add(table,BorderLayout.SOUTH);p1.setBounds(0, 0, 800, 800);p.removeAll();p.add(p1);//table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//是用来设置JTable的列宽度是否随着JTable的变化而变化。 // off固定不变。~}if(selectionNode.toString()=="显示用户信息") {JButton button1=new JButton("查看所有用户信息");JPanel p1=new JPanel();p1.setLayout(new java.awt.BorderLayout());JTable table; Object a[][];a=new Object[100][7];Object name[]= {"账号","姓名","性别","出生日期","电子邮箱","密码","已订票数"};table=new JTable(a,name);//a[99][3]="吴洪伟";for(int i=0;i<7;i++) {a[0][i]=name[i];}//滚动条JScrollPane jsp=new JScrollPane(table);p1.add(jsp,BorderLayout.SOUTH);p1.add(button1, BorderLayout.NORTH);jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//水平滚动条始终显示jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//垂直滚动条始终显示//p1.add(table,BorderLayout.SOUTH);p1.setBounds(0, 0, 800, 800);p.removeAll();p.add(p1);table.getColumnModel().getColumn(0).setPreferredWidth(100);//设置表格每列的宽度table.getColumnModel().getColumn(1).setPreferredWidth(80);table.getColumnModel().getColumn(2).setPreferredWidth(80);table.getColumnModel().getColumn(3).setPreferredWidth(80);table.getColumnModel().getColumn(4).setPreferredWidth(110);table.getColumnModel().getColumn(5).setPreferredWidth(80);table.getColumnModel().getColumn(6).setPreferredWidth(80);table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//是用来设置JTable的列宽度是否随着JTable的变化而变化。 // off固定不变。~}if(selectionNode.toString()=="用户咨询界面") {ChatWindow win=new ChatWindow();win.setDefaultCloseOperation(DISPOSE_ON_CLOSE);}}}}

这篇关于Java swing中树状组件JTree的使用,左边点击不同菜单,右边显示不同页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/372100

相关文章

Java Jackson核心注解使用详解

《JavaJackson核心注解使用详解》:本文主要介绍JavaJackson核心注解的使用,​​Jackson核心注解​​用于控制Java对象与JSON之间的序列化、反序列化行为,简化字段映射... 目录前言一、@jsonProperty-指定JSON字段名二、@JsonIgnore-忽略字段三、@Jso

MySQL中隔离级别的使用详解

《MySQL中隔离级别的使用详解》:本文主要介绍MySQL中隔离级别的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录引言undo log的作用MVCC的实现有以下几个重要因素如何根据这些因素判断数据值?可重复读和已提交读区别?串行化隔离级别的实现幻读和可

使用Python和SQLAlchemy实现高效的邮件发送系统

《使用Python和SQLAlchemy实现高效的邮件发送系统》在现代Web应用中,邮件通知是不可或缺的功能之一,无论是订单确认、文件处理结果通知,还是系统告警,邮件都是最常用的通信方式之一,本文将详... 目录引言1. 需求分析2. 数据库设计2.1 User 表(存储用户信息)2.2 CustomerO

Spring Validation中9个数据校验工具使用指南

《SpringValidation中9个数据校验工具使用指南》SpringValidation作为Spring生态系统的重要组成部分,提供了一套强大而灵活的数据校验机制,本文给大家介绍了Spring... 目录1. Bean Validation基础注解常用注解示例在控制器中应用2. 自定义约束验证器定义自

Java对接Dify API接口的完整流程

《Java对接DifyAPI接口的完整流程》Dify是一款AI应用开发平台,提供多种自然语言处理能力,通过调用Dify开放API,开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中,本... 目录Java对接Dify API接口完整指南一、Dify API简介二、准备工作三、基础对接实现1.

9个SpringBoot中的自带实用过滤器使用详解

《9个SpringBoot中的自带实用过滤器使用详解》在SpringBoot应用中,过滤器(Filter)是处理HTTP请求和响应的重要组件,SpringBoot自带了许多实用的过滤器,如字符编码,跨... 目录1. CharacterEncodingFilter - 字符编码过滤器功能和配置手动配置示例2

Redis持久化机制之RDB与AOF的使用

《Redis持久化机制之RDB与AOF的使用》:本文主要介绍Redis持久化机制之RDB与AOF的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis持久化机制-RDB与AOF一、RDB持久化机制1、RDB简介2、RDB的工作原理3、RDB的优缺点4

Spring Boot Controller处理HTTP请求体的方法

《SpringBootController处理HTTP请求体的方法》SpringBoot提供了强大的机制来处理不同Content-Type​的HTTP请求体,这主要依赖于HttpMessageCo... 目录一、核心机制:HttpMessageConverter​二、按Content-Type​处理详解1.

使用Python实现实时金价监控并自动提醒功能

《使用Python实现实时金价监控并自动提醒功能》在日常投资中,很多朋友喜欢在一些平台买点黄金,低买高卖赚点小差价,但黄金价格实时波动频繁,总是盯着手机太累了,于是我用Python写了一个实时金价监控... 目录工具能干啥?手把手教你用1、先装好这些"食材"2、代码实现讲解1. 用户输入参数2. 设置无头浏

Spring Boot 常用注解详解与使用最佳实践建议

《SpringBoot常用注解详解与使用最佳实践建议》:本文主要介绍SpringBoot常用注解详解与使用最佳实践建议,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、核心启动注解1. @SpringBootApplication2. @EnableAutoConfi