繁体   English   中英

JTree不会显示在JScrollPane中

[英]JTree does not display inside JScrollPane

我需要为我的程序显示帐户名称,我想在JScrollPane中使用JTree来完成此操作。

这是我的代码:

public void loadAccounts() {

    accountsRoot = new DefaultMutableTreeNode("Accounts"); //create root

    accountsRoot.add(new DefaultMutableTreeNode("Fred")); //add one element
                                                          //for testing
    accounts = new JTree(accountsRoot);

    accountsPane = new JScrollPane(accounts);

    accountsPane.add(accounts);  //don't think this is necessary
    canvas.add(accountsPane);
    accounts.setBounds(0, 0, accountsPane.getWidth(), accountsPane.getHeight());
    accountsPane.setBounds(460, 270, 240, 410);
    accounts.setVisible(true);
    accountsPane.setVisible(true);

}

因为我没有使用布局,所以我手动设置边界。

我似乎无法让它显示出来。 我想最终最终加载帐户一段时间,所以我觉得JTree很容易,

accountsPane = new JScrollPane(accounts);

accountsPane.add(accounts);  //don't think this is necessary

这不仅是必要的,而且会使事情变得混乱,因为这实际上将您的帐户JTree添加到多个容器 - JScrollPane的视口(好)和JScrollPane本身(坏)。 不要那样做。 只能通过JScrollPane的构造函数将其添加到JScrollPane的视口中,如上面第一行所示,或者在创建JScrollPane对象后调用setViewportView(...)

编辑:另一个问题是你使用setBounds(...) 您不应该这样做,而应该使用布局管理器来正确查看您的组件。 您还需要在接受JScrollPane的任何容器上调用revalidate()repaint()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM