![](/img/trans.png)
[英]how position a JDialog relative to a JPanel within a JFrame
[英]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();
是否有可能获取该位置在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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.