欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 17:44:01
本文实例讲述了swing中tree与滚动条用法。分享给大家供大家参考。具体如下: import java.awt.*; import java.awt.even...

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

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程序设计有所帮助。