zoukankan      html  css  js  c++  java
  • Swing组件Jtree,JTablePane选项卡运用

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

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

    至于其他,我高兴就好...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),字号18
    72         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 }

    预览效果

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

    拼了命的去努力
  • 相关阅读:
    Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs
    Codeforces Round #346 (Div. 2) E. New Reform dfs
    Codeforces Round #346 (Div. 2) D. Bicycle Race 叉积
    Codeforces Round #346 (Div. 2) C. Tanya and Toys 贪心
    Codeforces Round #346 (Div. 2) B. Qualifying Contest 水题
    Codeforces Round #346 (Div. 2) A. Round House 水题
    VK Cup 2016
    Entity Framework DBFirst尝试
    Entity Framework简介
    设计模式之十(外观模式)
  • 原文地址:https://www.cnblogs.com/chanke/p/4753810.html
Copyright © 2011-2022 走看看