gpt4 book ai didi

swing中Tree与滚动条用法实例分析

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章swing中Tree与滚动条用法实例分析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了swing中Tree与滚动条用法。分享给大家供大家参考。具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class test extends JApplet{
   JTree tree;
   JTextField jtf;
   public void init(){
     Container contentPane = getContentPane();
     contentPane.setLayout( new BorderLayout());
     DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Options" );
     DefaultMutableTreeNode a = new DefaultMutableTreeNode( "A" );
     top.add(a);
     DefaultMutableTreeNode a1 = new DefaultMutableTreeNode( "A1" );
     a.add(a1);
     DefaultMutableTreeNode a2 = new DefaultMutableTreeNode( "A2" );
     a.add(a2);
     DefaultMutableTreeNode b = new DefaultMutableTreeNode( "B" );
     top.add(b);
     DefaultMutableTreeNode b1 = new DefaultMutableTreeNode( "B1" );
     b.add(b1);
     DefaultMutableTreeNode b2 = new DefaultMutableTreeNode( "B2" );
     b.add(b2);
     tree = new JTree(top);
     int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
     int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
     JScrollPane jsp = new JScrollPane(tree,v,h);
     contentPane.add(jsp, BorderLayout.CENTER);
     jtf = new JTextField( "" , 20 );
     contentPane.add(jtf, BorderLayout.SOUTH);
     tree.addMouseListener( new MouseAdapter(){
       public void mouseClicked(MouseEvent me){
         doMouseclicked(me);
       }
     });
   }
   void doMouseclicked(MouseEvent me){
     TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
     if (tp != null )
       jtf.setText(tp.toString());
     else
       jtf.setText( "" );
   }
}

希望本文所述对大家的java程序设计有所帮助.

最后此篇关于swing中Tree与滚动条用法实例分析的文章就讲到这里了,如果你想了解更多关于swing中Tree与滚动条用法实例分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com