gpt4 book ai didi

java - 获取 JFrame 中 JTextField 的坐标

转载 作者:行者123 更新时间:2023-12-01 19:18:03 24 4
gpt4 key购买 nike

我使用 BorderLayoutJFrame 中添加了 JTextField
现在我想知道 JTextField 的位置。

我使用了 getX()getLocation(),但两者都给出了 0,这不是正确的答案。
那么我如何获得该位置?

最佳答案

在组件(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

关于java - 获取 JFrame 中 JTextField 的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com