简体   繁体   English

如何在JdesktopPane 的中央设置一个JinternalFrame 可见?

[英]How to setVisible a JinternalFrame in the center of the JdesktopPane?

Actually i wanna show the JinternalFrame in the center of the JDesktopPane and i used this methode as i use it in Jframes but it didn't work:实际上我想在 JDesktopPane 的中心显示 JinternalFrame 并且我使用了这个方法,因为我在 Jframes 中使用它但是它没有用:

Extraction ex=new Extraction();//Extraction is the name of the JintenalFrame jDesktopPane1.add(ex); ex.setLocationRelativeTo(this); ex.setVisible(true);

So i am asking if there is another methode so i can display the JinternalFrame in the center of the JdesktoPane.所以我问是否有另一种方法,以便我可以在 JdesktoPane 的中心显示 JinternalFrame。 Thank you谢谢

Try something like:尝试类似的东西:

JDesktopPane mainPanel;
JInternalFrame jif_test = new JInternalFrame();

public void centerJIF(JInternalFrame jif) {
    Dimension desktopSize = mainPanel.getSize();
    Dimension jInternalFrameSize = jif.getSize();
    int width = (desktopSize.width - jInternalFrameSize.width) / 2;
    int height = (desktopSize.height - jInternalFrameSize.height) / 2;
    jif.setLocation(width, height);
    jif.setVisible(true);
}

And

centerJIF(jif_test);

A javax.swing.JInternalFrame lacks the convenient setLocationRelativeTo(null) implementation found in java.awt.Window , but you can use a similar approach. javax.swing.JInternalFrame缺少在java.awt.Window中找到的方便的setLocationRelativeTo(null)实现,但您可以使用类似的方法。 Just subtract half the width and height of the internal frame from the corresponding center coordinates of the desktop pane.只需从桌面窗格的相应中心坐标中减去内部框架的宽度和高度的一半。 Use the new coordinates in your call to setLocation() .在调用setLocation()时使用新坐标。

You'll also want to adjust the internal frame's dimensions if necessary.如有必要,您还需要调整内部框架的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM