永发信息网

jtree 怎么做复杂的node

答案:1  悬赏:60  手机版
解决时间 2021-03-17 04:06
jtree 怎么做复杂的node
最佳答案
以下是一个关于JTree构建模型的例子,代码贴出来如下:

package day18_JTree;

import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
import javax.swing.tree. * ;

class TreeEditFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L ;
private DefaultTreeModel model;
private JTree tree;
private JButton addSiblingButton;
private JButton addChildButton;
private JButton deleteButton;

public TreeEditFrame() {
setTitle( " TreeEditTest " );
setSize( 300 , 200 );
this .pack();
this .setSize( 500 , 200 );
Toolkit tool = Toolkit.getDefaultToolkit();
double width = tool.getScreenSize().getWidth();
double height = tool.getScreenSize().getHeight();
this .setLocation((( int )(width - this .getWidth())) / 2 ,( int )(height - this .getHeight()) / 2 );
this .setVisible( true );
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TreeNode root = makeSampleTree();
model = new DefaultTreeModel(root);
tree = new JTree(model);
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon( new ImageIcon( " 1.gif " ));
renderer.setClosedIcon( new ImageIcon( " 2.gif " ));
renderer.setOpenIcon( new ImageIcon( " 3.gif " ));
// renderer.setBackgroundNonSelectionColor(Color.BLUE);
// renderer.setBackgroundSelectionColor(Color.RED);
renderer.setBorderSelectionColor(Color.RED);
tree.setCellRenderer(renderer);
Container contentPane = getContentPane();
JScrollPane scrollPane = new JScrollPane(tree);
contentPane.add(scrollPane, " Center " );
JPanel panel = new JPanel();
addSiblingButton = new JButton( " Add Sibling " );
addSiblingButton.addActionListener( this );
panel.add(addSiblingButton);
addChildButton = new JButton( " Add Child " );
addChildButton.addActionListener( this );
panel.add(addChildButton);
deleteButton = new JButton( " Delete " );
deleteButton.addActionListener( this );
panel.add(deleteButton);
contentPane.add(panel, " South " );
}

public TreeNode makeSampleTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode( " World " );
DefaultMutableTreeNode country = new DefaultMutableTreeNode( " USA " );
root.add(country);
DefaultMutableTreeNode state = new DefaultMutableTreeNode( " California " );
country.add(state);
DefaultMutableTreeNode city = new DefaultMutableTreeNode( " San Jose " );
state.add(city);
city = new DefaultMutableTreeNode( " Cupertino " );
state.add(city);
state = new DefaultMutableTreeNode( " Michigan " );
country.add(state);
city = new DefaultMutableTreeNode( " Ann Arbor " );
state.add(city);
country = new DefaultMutableTreeNode( " Germany " );
root.add(country);
state = new DefaultMutableTreeNode( " Schleswig-Holstein " );
country.add(state);
city = new DefaultMutableTreeNode( " Kiel " );
state.add(city);
return root;
}
public void actionPerformed(ActionEvent event) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (selectedNode == null )
return ;
if (event.getSource().equals(deleteButton)) {
if (selectedNode.getParent() != null )
model.removeNodeFromParent(selectedNode);
return ;
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode( " New " );
if (event.getSource().equals(addSiblingButton)) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode
.getParent();

if (parent != null ) {
int selectedIndex = parent.getIndex(selectedNode);
model.insertNodeInto(newNode, parent, selectedIndex + 1 );
}
} else if (event.getSource().equals(addChildButton)) {
model.insertNodeInto(newNode, selectedNode, selectedNode
.getChildCount());
}
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}
}
public class TreeEditTest {
public static void main(String[] args) {
JFrame frame = new TreeEditFrame();
frame.show();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
多样性 近义词
美的客户服务中心我想知道这个在什么地方
风中奇缘 石大伯为啥死啊?
王霞这个名字啥意思怎样解释最好
天天酷跑锦标赛奖励宝箱有多少彩钻
怎么看出对方什么时候把我旺旺拉黑的?有提示
软枣树怎么嫁接柿子牙
公积金个人交260,单位帮交260,请问我能贷款
Accidentsare______tohappeninsucha
金士顿(Kingston)骇客神条(KHX1600C9D3LK2
我在网上被网友骗走了8万元,只知道对方是广
华硕p8b75主板bios怎么开启
台湾史上地震伤亡人数
莎莎饰品地址在哪,我要去那里办事
iphone5s ios10哪个系统好
推荐资讯
潦倒的年轻人,这篇文章告诉了我们什么道理。
黄尖是什么鱼淮河有吗?
马犬上嘴长,下嘴短有影响吗
急!!盐城下面建湖县卖电脑的都在哪边了? 比
雨果把<<就英法联军远征中国给巴特勒上尉的信
佛山市禅城区人民法院南庄人民法庭地址在哪,
5.45+1.89-0.45的简便运算是什么?
kammy是男孩名还是女孩名?
形容辽阔的词语
地面微波是免费开放的吗
“予少时实有贪多之癖” 中的“予”是什么意
Chairman Maoto be unforgettable in the hea
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?