问题
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