java jframe调整大小

时间:2019-03-17 09:53:44

标签: java jframe resize

我有一个关于如何使两个JFrame大小相同的问题。当用户调整一侧的尺寸时,另一侧的尺寸也将得到调整,然后再调整尺寸(与数字一相同)。谢谢您的帮助。

    JFrame frame = new JFrame();    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DrawingPanel panel = new DrawingPanel();
    frame.setPreferredSize(new Dimension(windowWidth, windowHeight));
    frame.setMinimumSize(new Dimension(windowWidth, windowHeight));
    frame.setLocationRelativeTo(null);

    //resize both size at the same number
    int height = windowHeight;
    int widht = windowWidth;

    frame.addComponentListener(new ComponentAdapter() {  
            public void componentResized(ComponentEvent evt) {
                if (frame.getHeight() > windowHeight) {
                    frame.setSize(new Dimension(frame.getHeight(), frame.getHeight()));
                    frame.pack();
                }

                if (frame.getWidth() > windowWidth) {
                    frame.setSize(newDimension(frame.getWidth(), frame.getWidth()));
                }
            }
    });

    frame.add(panel);

    frame.pack();

    frame.setVisible(true);

0 个答案:

没有答案