如何获取屏幕中的 JFrame 位置以放入 JDialog?

How to get a JFrame position in the screen to put in a JDialog?

我想知道是否可以获取 JFrame 屏幕上的位置,以便将相同的位置放在 JDialog 中。我的意思是,我想做的是当我从 JFrame 按钮打开 JDialog 时,我想将 JDialog 放在与主 JFrame 相同的位置,也许使用 .setLocationRelativeto() 方法,有什么想法吗? ,谢谢!

   this.setUndecorated(true);
   initComponents();
   this.setBounds(WIDTH, WIDTH, 444, 308);
   JDialog dialog=new JDialog();

我想采用与 mainJFrame 相同的位置,但我不知道该怎么做....

   dialog.setLocationRelativeTo();  

is it possible to get the position of the location on the screen of a JFrame

ActionListener 中的代码类似于:

Component c = event.getComponent();
Window window = SwingUtilities.windowForComponent(c);
JDialog dialog = new JDialog(...);
...
dialog.pack();
dialog.setLocation( window.getLocation() );
dialog.setVisible( rue ;

或者您可以使用以下方法使对话框在框架上居中:

dialog.setLocationRelativeTo( window );
dialog.setVisible( true );