How do I open a JInternalFrame centered in a JDesktopPane?

后端 未结 5 1650
礼貌的吻别
礼貌的吻别 2021-01-05 09:21

I am adding a bunch of JInternalFrames into a JDesktopPane, as the user selects to open various features through the menus. But I would like the in

5条回答
  •  独厮守ぢ
    2021-01-05 09:56

    I would suggest the Window.setLocationRelativeTo(Component) method, which will center the window relative to a specified component. Instead of passing in a JDesktopPane, you might want to obtain the parent frame for a component, since otherwise, your JInternalFrame will be centered according to whichever component you pass in.

    Here is a code sample:

    private void showDialog(Dialog dialogToCenter, Component button) {
        Frame frame = JOptionPane.getFrameForComponent(button);
        dialogToCenter.setLocationRelativeTo(frame);
        dialogToCenter.setVisible(true);
    }
    

提交回复
热议问题