博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swing组件Jtree,JTablePane选项卡运用
阅读量:7281 次
发布时间:2019-06-30

本文共 6044 字,大约阅读时间需要 20 分钟。

今天开始写技术博客,说实话,本没有什么技术,说是总结也好,说是分享也罢,总之是想自己有意识的做一些事情,作为一名即将毕业的大学生,总是想以最好的状态,去面向社会,今天就是我准备好了的时候,本人将技术博客发布在新浪博客以及博客园,新浪博客,不仅发布技术博客,还会写一些个人随笔和感悟。而博客园,全是技术干货。希望大家视自己的情况关注。感谢么么哒!

 

技术博客,每周一篇。周一发布。

至于其他,我高兴就好...0.0...

一、Swing中JTree

 

 
1 package com.no1;  2   3 import java.awt.BorderLayout;  4 import java.awt.Dimension;  5   6 import javax.swing.JFrame;  7 import javax.swing.JOptionPane;  8 import javax.swing.JPanel;  9 import javax.swing.JTree; 10 import javax.swing.UIManager; 11 import javax.swing.event.TreeSelectionEvent; 12 import javax.swing.event.TreeSelectionListener; 13 import javax.swing.tree.DefaultMutableTreeNode; 14  15 public class JTreeExample extends JFrame { 16  17     /** 18      *  19      */ 20     private static final long serialVersionUID = 1L; 21     private JPanel Jp; 22  23     public static void main(String[] args) { 24  25         @SuppressWarnings("unused") 26         JTreeExample Je = new JTreeExample(); 27  28     } 29  30     // 构造函数 31     public JTreeExample() { 32         this.setTitle("JTree实例"); 33         // 34         this.setSize(200, 500); 35  36         // 窗口自动居中 37         this.setLocationRelativeTo(null); 38  39         this.setLayout(new BorderLayout()); 40  41         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 42         // 保持Window窗体基本风格 43         try { 44             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 45         } catch (Exception err) { 46             err.printStackTrace(); 47         } 48         // 不能最大化 49         this.setResizable(false); 50         this.init(); 51         this.setVisible(true); 52  53     } 54  55     public void init() { 56         // 实例化JPanel对象 57         Jp = new JPanel(); 58         // 设置大小 59         Jp.setPreferredSize(new Dimension(200, 400)); 60         // 为面板设置布局方式 61         Jp.setLayout(null); 62         // Jp.setBackground(Color.BLACK); 63  64         // 创造默认节点 65         DefaultMutableTreeNode note1 = new DefaultMutableTreeNode("中国"); 66         // 将实例化的节点加入以上节点 67         note1.add(new DefaultMutableTreeNode("北京")); 68         note1.add(new DefaultMutableTreeNode("上海")); 69         DefaultMutableTreeNode note2 = new DefaultMutableTreeNode("美国"); 70         note2.add(new DefaultMutableTreeNode("华盛顿")); 71         note2.add(new DefaultMutableTreeNode("纽约")); 72  73         DefaultMutableTreeNode top = new DefaultMutableTreeNode("国家"); 74         // 又将以上两个节点,note1与弄=note2加入到top节点 75         top.add(note1); 76         top.add(note2); 77         // 实例化JTree,并将top加入到JTree中 78         final JTree Jtree = new JTree(top); 79         // 设置Jtree大小 80         Jtree.setBounds(0, 0, 200, 300); 81         // 将Jtree入到面板中 82         Jp.add(Jtree); 83         // 将面板加入JFrame 84         this.add(Jp, BorderLayout.WEST); 85  86         // 为节点设置点击事件 87         Jtree.addTreeSelectionListener(new TreeSelectionListener() { 88  89             @Override 90             public void valueChanged(TreeSelectionEvent arg0) { 91  92                 /* 93                  * 返回当前选择的第一个节点中的最后一个路径组件。API原话 94                  * 就是返回你点击的节点 95                  */ 96                 DefaultMutableTreeNode node = (DefaultMutableTreeNode) Jtree.getLastSelectedPathComponent(); 97  98                 if (node == null) { 99                     return;100                 }101 102                 // 判断是否是一个子节点103                 if (node.isLeaf()) {104                     if ((node.toString()).equals("北京")) {105 106                         JOptionPane.showMessageDialog(null, "你点击的是北京");107 108                     } else if ((node.toString()).equals("纽约")) {109 110                         JOptionPane.showMessageDialog(null, "你点击的是纽约");111 112                     }113                 }114                 //如果为父节点添加事件,直接这样115                 if (node.toString().equals("中国")) {116 117                     JOptionPane.showMessageDialog(null, "你点击的是中国");118 119                 }120 121             }122         });123 124     }125 126 }
 

 

 

 

效果图

二、Swing组件之JTablePane选项卡

 

1 package com.no1; 2  3  4  5  6  7 import java.awt.Font; 8  9 10 import javax.swing.JFrame;11 import javax.swing.JPanel;12 import javax.swing.UIManager;13 import javax.swing.JTabbedPane;14 15 public class JTabbedPaneExample extends JFrame{16 17     private JTabbedPane aa ;18     private JPanel  Jp01,Jp02,Jp03;19     20     private static final long serialVersionUID = 1L;21     22     public static void main(String[] args) {23         @SuppressWarnings("unused")24         JTabbedPaneExample a =new JTabbedPaneExample();25 26     }27     public JTabbedPaneExample(){28 29         30         this.setTitle("JTablePane实例");31         //32         this.setSize(500, 300);33 34         // 窗口自动居中35         this.setLocationRelativeTo(null);36 37         this.setLayout(null);38 39         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);40         // 保持Window窗体基本风格41         try {42             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());43         } catch (Exception err) {44             err.printStackTrace();45         }46         // 不能最大化47         this.setResizable(false);48         this.init();49         50         51         this.setVisible(true);52         53         54     }55     private void init(){56         57         aa =new JTabbedPane(JTabbedPane.TOP);58         //给JTabbedPane设置大小59         aa.setBounds(0, 0, 500, 300);60         Jp01 =new JPanel();61         Jp02 =new JPanel();62         Jp03 =new JPanel();63     64         Jp01.setBounds(0, 0, 500, 300);65         Jp02.setBounds(0, 0, 500, 300);66         Jp03.setBounds(0, 0, 500, 300);67         //将三个面板加入到JTabbedPane上68         aa.addTab("面板一", Jp01);69         aa.addTab("面板二", Jp02);70         aa.addTab("面板三", Jp03);71         //设置字体为宋体,不加粗(加粗为1),字号1872         aa.setFont(new Font("宋体", 0, 18));73         //添加到JFrame内容面板上,也可以直接this.add(aa);74         this.getContentPane().add(aa);75         //初始显示面板一76         aa.setSelectedIndex(0);77         78     }79     80 }

 

预览效果

好了,接下来看你们的了。

 

转载于:https://www.cnblogs.com/chanke/p/4753810.html

你可能感兴趣的文章
使用inotify监视Linux文件变化
查看>>
SQLite第八课 auth.c授权文件解析
查看>>
nginx日志切割
查看>>
配置Samba服务器
查看>>
AIX内存性能优化和监视
查看>>
haproxy根据客户端浏览器进行跳转
查看>>
12c DataGuard switchover to 'primary'
查看>>
Django返回json数据
查看>>
在Linq to Entity 中使用lambda表达式来实现Left Join和Join
查看>>
Linux自学笔记——http协议进阶及httpd配置(2)
查看>>
Linux必会原理之软连接文件和硬链接文件的区别
查看>>
ORACLE 12c常见问题解决
查看>>
查看安装的tensorflow版本号和路径
查看>>
依靠自学,争取帮助
查看>>
利用Zabbix的自动注册功能添加局域网中的服务器进行监控
查看>>
system center 详解
查看>>
shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向
查看>>
在SCCM中配合 WSUS后,显示: 客户端尚未报告
查看>>
朴素的UNIX之-调度器前传
查看>>
压力测试与提升服务器能力的几个方法
查看>>