我有一个 Jpanel,我在其中添加了许多组件(比如 100 个 JButton)。 当我现在请求添加的组件的高度时,它们都是 0。 代码来了:
void AddComponents(){
//add 100 Buttons to my jpanel
for (Component component : jpanel.getComponents()) {
Rectangle test = component.getBounds();
DebugTextArea.append("\nx: " + test.x + "- y:" + test.y +
"- height: " + test.height + "- width: " + test.width);
}
}
所有值均为 0。我知道这与组件的绘制需要一些时间有关,但我如何获得高度,因为我需要组件的高度来将 VerticalScrollBar 设置为某一点。
有什么想法吗?
最佳答案
我需要组件的高度来设置 VerticalScrollBar - 不,你不需要(假设这是一个 JScrollPane 的滚动条,它包含带有按钮的面板
button.scrollRectToVisible(button.getBounds())
组件实现后(又名:显示)
关于java - JComponent 绘制前高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8522765/