检测 JPanel 大小变化的来源
我有一个名为 TablePanel 的类,源自 JPanel。当我单击框架另一部分中的复选框时,此类的实例有时会调整自身大小。
是否有某种事件侦听器或其他东西,以便我可以追踪实际导致此 TablePanel 调整大小的事件?
我尝试将以下内容添加到 TablePanel 的构造函数中:
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("TablePanel evt: " + evt);
}
});
但这在调整大小时不会“触发”。
I have a class called TablePanel descended from JPanel. An instance of this class is resizing itself sometimes when I click on a check box that is in another part of the frame.
Is there some sort of event listener or something so I can track down what event is actually causing this TablePanel to resize?
I have tried adding the following to the constructor of TablePanel:
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("TablePanel evt: " + evt);
}
});
but this isn't 'firing' when the resize happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您正在寻找 ComponentListener 的 componentResized 事件。
虽然我不确定你为什么需要这个。面板的布局管理器应该处理面板上任何组件的大小调整/布局。
这不会为您提供更改的实际来源。源事件可能是调整框架大小,然后布局管理器完成其工作。
我从未见过这种情况发生。单击复选框不应更改其大小,因此面板上的组件没有理由再次布局。你一定有一些奇怪的代码。发布您的 SSCCE 来演示该问题。
I guess you are looking for the componentResized event of the ComponentListener.
Although I'm not sure why you need this. The layout manager of your panel should handle this the resizing/layout of any components on the panel.
This won't give you the actual source of the change. The source event is likely the frame being resized and then the layout managers do their job.
I have never seen this happen. Clicking on a checkbox should not change its size, therefore there is no reason for components on the panel to be layed out again. You must have some strange code. Post your SSCCE that demonstrates the problem.