简体   繁体   English

您如何获得JPanel引入的边距

[英]How do you get the margins introduced by a JPanel

I have a JPanel panel which contains a JTree tree . 我有一个JPanel 面板 ,其中包含一个JTree Sometimes the JTree has width or height greater then the panel itself. 有时,JTree的宽度或高度大于面板本身。

What I want to do is recalculate the height and width of tree using the height and width of panel. 我要做的是使用面板的高度和宽度重新计算树的高度和宽度。 The only problem is that I don't know how to get the margins introduced by a JPanel in order to precisely calculate the new Size of the JTree. 唯一的问题是,我不知道如何获得JPanel引入的边距来精确计算JTree的新大小。

Example of what i want: 我想要的例子:

if (treeHeight >= panelSize.getHeight()) {
    treeHeight = panelSize.getHeight() - panelMargins; // panelMargins is what I don't know how to calculate
}

UPDATE : 更新:

  1. My JTree is already in a JScrollPane 我的JTree 已经在JScrollPane中
  2. My JScrollPane is inside the Jpanel 我的JScrollPane在Jpanel内

you have to use 你必须使用

1) proper LayoutManager 1)适当的LayoutManager

2) add EmptyBorder 2)添加EmptyBorder

then you never need that 那么你永远不需要

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

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