【问题标题】:JComponent height before paintingJComponent 绘制前的高度
【发布时间】:2012-01-21 07:21:49
【问题描述】:

我有一个 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 设置为某一点。

有什么想法吗?

【问题讨论】:

  • 请学习java命名约定并遵守它们
  • sry 下次我会努力做得更好

标签: java swing components jpanel paint


【解决方案1】:

我需要组件的高度来设置 VerticalScrollBar - 不,你不需要(假设这是 JScrollPane 的滚动条,其中包含带有按钮的面板

  button.scrollRectToVisible(button.getBounds())

组件实现后(又名:如图)

【讨论】:

  • 已实现/显示是问题...我添加了按钮,之后仅 2 行(在同一函数中)我将请求大小...并且它响应 0。(你是对的JPanel 在 JScrollPane 中)
猜你喜欢
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2010-10-29
  • 2021-06-14
  • 1970-01-01
相关资源
最近更新 更多