![](/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.