[英]Read JFrame size from JPanel
I have a Layout class which extends JFrame
. 我有一个Layout类,它扩展了
JFrame
。 Later I am adding RoadPanel
which extends JPanel
. 后来我添加了
RoadPanel
,它扩展了JPanel
。 At run-time is it possible to know Layout
frame size in RoadPanel
? 在运行时是否可以知道
RoadPanel
Layout
框架大小?
Code 码
class Layout extends JFrame{
...Something...
RoadPanel rp = new RoadPanel();
this.add(rp);
}
class RoadPanel extends JPanel{
...graphics and components...
}
Both are implemented in different Class. 两者都在不同的类中实现。
RoadPanel
.
RoadPanel
的构造函数中。
Yeah, that is so lame as it will send only once while initiated.
How to make this dynamic so it updates continuously? 如何使其动态,使其不断更新?
Why I want to do this? 为什么我要这样做? It because...
因为...
Purpose of knowing frame size is to scale down my Lines and Arcs of RoadPanel :) But I didn't wanted scale up though.
知道框架大小的目的是缩小我的RoadPanel的线和弧:)但我不想放大。 So now I can check if frame size is less than my path.getBounds2D than repaint all line and arc and fit into visible area.
因此,现在我可以检查框架大小是否小于path.getBounds2D,然后重新绘制所有直线和圆弧并适合可见区域。 But when you increase frame size, RoadPanel drawing should remain same and can be zoomed-in by manually using zoom button.
但是,当您增加框架大小时,RoadPanel图形应保持不变,并且可以通过使用缩放按钮手动进行放大。
Special pointer towards Andrew Thompson's answer, For better practise. 特别提示安德鲁·汤普森(Andrew Thompson)的答案:更好的练习。
..is it possible to know Layout frame size in RoadPanel?
..是否可以知道RoadPanel中的布局框架大小?
Use a layout (or layouts). 使用一个或多个布局。 Then it won't be necessary to know the size of the parent container.
这样就不必知道父容器的大小。
See Laying Out Components Within a Container for details. 有关详细信息,请参见在容器中布置组件 。
How to make this dynamic so it updates continuously?
如何使其动态,使其不断更新?
The layouts will update the sizes and positions of components automatically. 布局将自动更新组件的大小和位置。 For custom painting, simply call
Container.getSize()
at time of paintComponent()
. 对于自定义绘画,只需在
paintComponent()
时调用Container.getSize()
即可。
However purpose of knowing frame size is to scale down my Lines and Arcs of RoadPanel
但是,了解框架大小的目的是缩小我的RoadPanel的线和弧
In case I was not clear enough above: For custom painting, simply call Container.getSize()
at time of paintComponent()
. 如果上面我还不够清楚: 对于自定义绘画,只需在
paintComponent()
时调用Container.getSize()
即可。 In this instance, the Container
would be RoadPanel
. 在这种情况下,
Container
将是RoadPanel
。 Incidentally, this will work correctly irrespective of: 顺便说一句,无论以下情况如何,此方法都可以正常工作:
RoadPanel
having a Border
. Border
RoadPanel
。 Frame
or not being the root component. Frame
或不是根组件。 You can either pass the frame itself to the panel constructor and later query the size, or you might use getParent()
which should return the direct parent of the panel. 您可以将框架本身传递给面板构造函数,然后再查询尺寸,也可以使用
getParent()
来返回面板的直接父级。 In that case it might be necessary to move further up the hierarchy until you reach the frame. 在这种情况下,可能有必要进一步向上移动层次结构,直到到达框架为止。
Edit: depending on what you want to achieve, I'd suggest you first try and use Andrew's suggestions and use layout managers. 编辑:根据您要实现的目标,建议您首先尝试使用安德鲁的建议并使用布局管理器。 You should use the parent's size directly only if the layout manager doens't provide the functionality you need (note that the way that functionality is provided might be different than what you expect).
仅当布局管理器未提供所需功能时,才应直接使用父级的尺寸(请注意,提供功能的方式可能与您期望的方式不同)。
从RoadPanel
尝试:
this.getParent().getSize();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.