How to setVisible a JinternalFrame in the center of the JdesktopPane?

China☆狼群 提交于 2019-12-11 06:37:10

问题


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 :

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. Thank you


回答1:


A javax.swing.JInternalFrame lacks the convenient setLocationRelativeTo(null) implementation found in java.awt.Window, but you can use a similar approach. 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().

You'll also want to adjust the internal frame's dimensions if necessary.




回答2:


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);


来源:https://stackoverflow.com/questions/10046581/how-to-setvisible-a-jinternalframe-in-the-center-of-the-jdesktoppane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!