我在JDesktopPane
中添加了一堆JInternalFrame
,因为用户选择通过菜单打开各种功能。但我希望内部框架打开在桌面窗格中心,而不是左上角,他们似乎默认。如何打开以JDesktopPane为中心的JInternalFrame?
如何指定JInternalFrame打开居中,或将它们移到打开后的中心?
jDesktopPane.add(jInternalFrame); // jInternalFrame is not centered!
我在JDesktopPane
中添加了一堆JInternalFrame
,因为用户选择通过菜单打开各种功能。但我希望内部框架打开在桌面窗格中心,而不是左上角,他们似乎默认。如何打开以JDesktopPane为中心的JInternalFrame?
如何指定JInternalFrame打开居中,或将它们移到打开后的中心?
jDesktopPane.add(jInternalFrame); // jInternalFrame is not centered!
仅供参考,这里是我以前的基础上,罗布麻的建议解决方案:
Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = jInternalFrame.getSize();
jInternalFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2,
(desktopSize.height- jInternalFrameSize.height)/2);
制定出新位置的左上角(基于JDesktopPane
和JInternalFrame
的大小),然后调用JInternalFrame.setLocation
。
我会建议Window.setLocationRelativeTo(Component)方法,它将窗口相对于指定组件的中心。除了传递JDesktopPane中,你可能想获得一个组件父框架,否则,你的JInternalFrame将居中根据您在通过任何组件中
下面是一个代码示例:
private void showDialog(Dialog dialogToCenter, Component button) {
Frame frame = JOptionPane.getFrameForComponent(button);
dialogToCenter.setLocationRelativeTo(frame);
dialogToCenter.setVisible(true);
}
你应该得到与和屏幕的高度,并提出一些数学来设置JInternalFrame的X和Y。我多年没有这样做,所以我不能给你完整的方法。对不起 – santiagobasulto