![](/img/trans.png)
[英]JTextField is not getting resized when I try to resize the JFrame
[英]Getting coordinates of a JTextField in a JFrame
我已经使用BorderLayout
在JFrame
添加了JTextField
。
现在我想知道JTextField
的位置。
我使用了getX()
和getLocation()
,但是都给了我0
,这不是正确的答案。
那我怎么去那个位置呢?
getLocation()方法在内容窗格中提供了组件相对于其父组件的位置,因此该位置实际上可能是0。此外,在框架可见之前,您不能调用该方法。
如果要相对于框架的位置,则需要使用
SwingUtilities.convertPoint(...);
在组件( JTextField
)中调用getLocation()
应该返回其相对于父容器的坐标(在下面的示例中为JFrame
)。
注意 : 在获取坐标之前 ,必须在容器( JFrame
)中进行 布局 。
仅显示布局即可完成布局。
范例 :
JFrame fr = new JFrame("Testing window");
JTextField tf = new JTextField();
fr.add(tf, BorderLayout.CENTER);
fr.setVisible(true); //show the JFrame
Point p = tf.getLocation(); //get the coordinates
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.