在WTK2.5 中实现层次树(treeview)构件

WTK提供的用户界面构件实在是太不给力了,应付从前的小屏幕还将就能用,现在手机的大屏能显示丰富的信息,大量复杂的层次信息常常需要使用TREEVIEW,既然没有现成的,就只好自己做一个。

以下代码是我从网上搜来的,然后根据自己的需要作了一番改造。

 

节点类,节点当中有一个存取子节点的向量,以及对子节点操作的一组方法

 

 

import java.util.Vector;

 

 

public class TreeNode {

 

    public int x = 0;

    public int y = 0;

 

    public TreeNode parentNode = null;

    public String label = null;

    public String value = null;

    public boolean expanded = false;

    public String iconName = "";

    public int index = 0;

    Vector children = null;

 

    public MenuNode(String label) {

        this.label = label;

        this.value = label;

        this.children = new Vector();

    }

 

    public MenuNode(String label, String value) {

        this.label = label;

        this.value = value;

        this.children = new Vector();

    }

 

    public void appendChild(MenuNode node) {

        node.parentNode = this;

 

        node.index = this.children.size();

 

        this.children.addElement(node);

    }

 

    public int getChildrenNum() {

        return this.children.size();

    }

 

    public MenuNode getNextSibling() {

        if (parentNode != null) {

            return parentNode.getChild(index + 1);

        }

        return null;

    }

 

    public MenuNode getPrevSibling() {

        if (parentNode != null) {

            return parentNode.getChild(index - 1);

        }

        return null;

    }

 

    public MenuNode getLastChild() {

        return getChild(getChildrenNum() - 1);

    }

 

    public MenuNode getChild(int i) {

        if (i >= 0 && i < this.children.size()) {

            return (MenuNode) this.children.elementAt(i);

        }

        return null;

    }

 

    public void removeChildren(int index) {

        this.children.removeElementAt(index);

 

        for (int i = index; i < this.children.size(); i++) {

            this.getChild(i).index--;

        }

    }

 

    public boolean hasChildren() {

        return this.getChildrenNum() > 0;

    }

 

    public void expand() {

        if (this.getChildrenNum() > 0) {

            this.expanded = true;

        }

    }

 

    public void collapse() {

        this.expanded = false;

    }

}

层次树类,画出层次树,响应用户事件
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class TreeView extends CustomItem {
    Font font = Font.getDefaultFont();
    int foreColor = 0x000000FF;  //字体颜色
    int alternateForeColor = 0x00000000;
    int foreFocusedColor = 0x00CCCCCC; //选中节点的字体颜色
    int backFocusedColor = 0x000000FF;  //选中节点的背景颜色
    int viewportHeight = 200; //树在屏幕上的高度
    int viewportWidth = 200; //书在屏幕上的宽度
    int viewportY = 0;
    int nodeHeight = 0;
    int childPadding = 10;
    int verticalPadding = 2;
    private Vector rootNodes = null;
    TreeNode current = null;
    Image expandIcon = null;
    Image collapseIcon = null;
    Image leafIcon = null;
    Image alternateLeafIcon = null;
    public static final int ICON_WIDTH = 6;
    public static final int ICON_HEIGHT = 6;
    public TreeView() {
        super("");
        try {
            expandIcon = Image.createImage(getClass().getResourceAsStream("/plus.png"));
            collapseIcon = Image.createImage(getClass().getResourceAsStream("/minus.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值