gpt4 book ai didi

java - 在布局内的组件上 Swing getLocationOnScreen 方法

转载 作者:行者123 更新时间:2023-12-02 05:21:23 27 4
gpt4 key购买 nike

需要帮助:

我尝试对放置在 FlowLayout 内的每个组件调用 getLocationOnScreen() 并最终获得相同的位置。

这是设计使然还是我做错了什么?那么我应该如何获取每个组件的确切位置?

这是我刚刚创建的示例

JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
frame.getContentPane().add(panel);

for (int i = 0; i < 4; i++) {
JLabel label = new JLabel("Label " + i);
panel.add(label);
System.out.println("getLocationOnScreen-" + i
+ " x:" + label.getLocationOnScreen().x
+ " y:" + label.getLocationOnScreen().y);
}

它在控制台中打印为:

getLocationOnScreen-0 x:0 y:44
getLocationOnScreen-1 x:0 y:44
getLocationOnScreen-2 x:0 y:44
getLocationOnScreen-3 x:0 y:44

如果您能对此提供帮助,我们将不胜感激!谢谢!

最佳答案

基于this comment :

... the actual use case would be showing a ballon popup related to the label in the layout.

您似乎有一个 XY problem :您正在询问组件在屏幕上的位置,但您真正想做的是为每个标签显示一个弹出气球。

Swing 内置了这样的功能,称为工具提示。看看How to Use Tool Tips学习如何使用它们的类(class)。

此外,正如 @peeskillet 建议的那样,有一个名为 Balloon tip 的第三方库可用。值得一看。

关于java - 在布局内的组件上 Swing getLocationOnScreen 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488897/

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