我搜索一个方法,如果 jpanel 显示在显示屏上,该方法就会被调用,因为我必须获取面板的实际大小。
有什么建议吗?
最佳答案
您是否尝试过添加 ComponentListener到 JPanel?这就是我在 componentShown(...)
方法中开始编写代码的地方。为此,我认为您必须在将 JPanel 添加到显示后调用 setVisible(true)
。
另一个选项是在 GUI 上调用 pack()
或 setVisible(true)
后简单地查询其大小。
编辑
您声明:
I added the panel to the gui designer.. when the window pops up, i wanna now the real size of the jpanel, because it can change it.
如果您想知道“当窗口弹出时”窗口所持有的组件的大小,请添加 WindowListener到窗口并通过 windowOpened(...)
方法检查组件的大小。
编辑2
然后你说:
after i have the real size, i add some subpanels, in relation to the size of this panel. so e.g. size/6 & the subpanel has now the size-height of size/6.
一个解决方案:最好不要设置任何东西的大小,而是使用布局管理器的正确组合,然后让他们根据自己的规则设置适当的大小。
关于java - JPanel ActionMethod 显示时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153699/