[英]How to get absolute position of any component in any layout Java Swing?
Maybe this is a stupid question but I lose hours trying it. 也许这是一个愚蠢的问题,但我失去了几个小时的尝试。 I have any JFrame with her components inside.
我有任何内部组件的JFrame。 This JFrame can have any Layout.
这个JFrame可以有任何布局。 My question is simple I have this window: In the image I use absolute layout to know the real location.
我的问题很简单我有这个窗口:在图像中我使用绝对布局来了解真实位置。
I want know the position of the button. 我想知道按钮的位置。 I'm using a GridBagLayout and when I do button.getY() or button.getX() , I have 0 ever, in X or Y. But if I use absolute layout, I have not problem.
我正在使用GridBagLayout,当我执行button.getY()或button.getX()时,我有0个,在X或Y.但如果我使用绝对布局,我没有问题。
If I use absoluteLayout I know that I have not this problem. 如果我使用absoluteLayout,我知道我没有这个问题。 But I want use any layout and know the absolute position of any component in the frame.
但我想使用任何布局并知道框架中任何组件的绝对位置。
Really I want to do a autocomplete textField ( like google when you are writing to search ), when I write in the TextField , I want show other window jframe below textfield with the coincidences. 我真的想做一个自动完成的textField(比如google,当你写入搜索时),当我在TextField中写字时,我想在文本字段下方显示其他窗口jframe的巧合。
If I know the absolute position of any textfiel, I can calculate the position and show the new window. 如果我知道任何文本的绝对位置,我可以计算位置并显示新窗口。
Thanks to all. 谢谢大家。
One of the SwingUtilities.convertPoint()
methods should do what you want. 其中一个
SwingUtilities.convertPoint()
方法应该做你想要的。 The following example prints java.awt.Point[x=54,y=22]
. 以下示例打印
java.awt.Point[x=54,y=22]
。
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JLabel("Test"), BorderLayout.WEST);
JButton b = new JButton("Test");
f.add(b);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
System.out.println(SwingUtilities.convertPoint(b, b.getX(), b.getY(), f));
我认为本教程对你提出的问题有一个答案... http://javadevwannabe.blogspot.gr/2012/02/using-absolute-positioning.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.