如何在JScrollPane对象中设置insets?

时间:2012-08-07 08:09:58

标签: java swing jscrollpane jtree insets

我有JScrollPane,它只包含一个JTree实例。如何在JScrollPane中设置JTree周围的边距?

我有这样的代码

tree.setPreferredSize(new Dimension(200, 200));
    JScrollPane scrollerTree = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollerTree.setPreferredSize(new Dimension(200, 199));
    scrollerTree.getVerticalScrollBar().setUnitIncrement(16);

我想实现,利用滚动窗格内容可以滚动边距。

1 个答案:

答案 0 :(得分:12)

更新:根据您更新的问题,我认为您实际上是在JComponent.setBorder之后,示例屏幕截图:

screenshot border

代码:

public static void main(String[] args) {

    JTree tree = new JTree();
    tree.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    JFrame frame = new JFrame("Test");
    frame.add(new JScrollPane(tree));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

原始答案,如果您想要查看端口周围的边框,请使用JScrollPane.setViewportBorder

screenshot viewport border

代码:

public static void main(String[] args) {

    JScrollPane scroll = new JScrollPane(new JTree());
    scroll.setViewportBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    JFrame frame = new JFrame("Test");
    frame.add(scroll);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}