开发者

Opening a new jframe with the same properties as the closed one

开发者 https://www.devze.com 2023-03-09 04:13 出处:网络
How can I have the newly open jframe have the same properties开发者_运维百科 like Size and position on screen. ThanksIf you are only interested in size and position JFrame.getBounds returns these prop

How can I have the newly open jframe have the same properties开发者_运维百科 like Size and position on screen. Thanks


If you are only interested in size and position JFrame.getBounds returns these properties:

newFrame.setBounds(oldFrame.getBounds());

public static void main(String args[]) throws Exception {

    final JFrame oldFrame = new JFrame("Test");

    oldFrame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            JFrame newFrame = new JFrame("Test");
            newFrame.setBounds(oldFrame.getBounds());
            newFrame.setVisible(true);
        }
    });
    oldFrame.setSize(400, 300);
    oldFrame.setVisible(true);
}


@Sam that not good idea to create new TopLevelContainer on fly, better would be reuse exist JFrame and just replace/switch its contents JPanel and if you need/want to display more TopLevelContainers then others would by JDialog(s)

please check how to LayoutManagers works (with examples there) and tons of example about Swing on Java2s.com

0

精彩评论

暂无评论...
验证码 换一张
取 消