在JFrame最大化时调整JPanel的大小

时间:2015-01-08 22:40:03

标签: java resize jframe jpanel maximize

好的,所以下面的代码在首次运行程序时显示JFrame中的JPanel。如果通过拖动框架的一个边或角来重新调整窗口的大小,则JPanel会自行调整大小并保持监视器的纵横比。

注意: JPanel设置为仅在1920x1080分辨率监视器上保持在窗口范围内。在任何其他显示器尺寸上,JPanel可能会被切断。请参阅updatePanelSize()方法中的setPreferredSize()上面的注释。

public class Frame extends JFrame {

    Panel panel = new Panel();

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override 
            public void run() { 
                new Frame(); 
            } 
        }); 
    }

    // Setup the window, add the panel, and initialize a "window" listener.
    public Frame() {            
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1280, 720);
        setLocationRelativeTo(null);
        setVisible(true);
        setTitle("Frame");
        setLayout(new GridBagLayout());

        add(panel);
        initListeners();
    }

    public void initListeners() {

        /** When the window is resized, the panel size is updated. */
        addComponentListener(new ComponentListener() {

            @Override
            public void componentResized(ComponentEvent e) {        
                panel.updatePanelSize();
            }

            @Override
            public void componentHidden(ComponentEvent evt) {}

            @Override
            public void componentShown(ComponentEvent evt) {}

            @Override
            public void componentMoved(ComponentEvent evt) {}
        });
    }
}

public class Panel extends JPanel {

    public Panel() {
        setBackground(new Color(100, 0, 0));
        setPreferredSize(new Dimension(1052, 592));
    }

    // Resizes the JPanel while maintaining the same aspect ratio
    // of the monitor.
    public void updatePanelSize() { 

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        float monitorWidth = gd.getDisplayMode().getWidth();
        float monitorHeight = gd.getDisplayMode().getHeight();

        // Aspect ratio of the monitor in decimal form.
        float monitorRatio = monitorWidth / monitorHeight;

        JComponent parent = (JComponent) getParent();
        float width = parent.getWidth();
        float height = parent.getHeight();

        width = Math.min(width, height * monitorRatio);
        height = width / monitorRatio;

        // I am subtracting the width and height by their respected aspect ratio
        // coefficients (1920x1080 -> 16:9 (width:height)) and multiplying them 
        // by some scale (in this case 10) to add a "padding" to the JPanel.
        // The ratio coefficients and scale will need to be edited based upon the
        // resolution of your monitor.
        setPreferredSize(new Dimension((int)width - (16 * 10), (int)height - (9 * 10)));

        System.out.println("PanelRes: " + ((int)width - (16 * 10)) + "x" + ((int)height - (9 * 10)));
        System.out.println("PanelRatio: " + getWidth() / getHeight());
    }
}

我遇到的问题是,如果我通过双击窗口工具栏(或窗口顶部的正确术语)或通过单击最大化按钮来最大化窗口,则JPanel不会重新调整大小喜欢它应该。当窗口最大化时,将调用重写的componentResized()方法,但JPanel不会调整大小。解决这个问题的任何帮助都会很棒。

1 个答案:

答案 0 :(得分:3)

在调整大小时,面板会立即接受updatePanelSize()中的新首选尺寸,但在最大化/恢复时,面板显然忽略了新的首选尺寸。

我已添加了对revalidate()的调用,强制小组在没有应用新的首选维度的情况下进行更新。

public void updatePanelSize() {

    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice();
    float monitorWidth = gd.getDisplayMode().getWidth();
    float monitorHeight = gd.getDisplayMode().getHeight();

    // Aspect ratio of the monitor in decimal form.
    float monitorRatio = monitorWidth / monitorHeight;

    JComponent parent = (JComponent) getParent();
    float width = parent.getWidth();
    float height = parent.getHeight();

    width = Math.min(width, height * monitorRatio);
    height = width / monitorRatio;

    // I am subtracting the width and height by their respective aspect ratio...
    int paddedWidth = (int) width - (16 * 10);
    int paddedHeight = (int) height - (9 * 10);
    setPreferredSize(new Dimension(paddedWidth, paddedHeight));

    int resultWidth = getWidth();
    int resultHeight = getHeight();
    if (paddedWidth != resultWidth && paddedHeight != resultHeight) {
        revalidate(); // preferred dimensions not applied, so force them
    }

    System.out.println("PreferredSize: " + paddedWidth + "x" + paddedHeight);
    System.out.println("PanelRes: " + resultWidth + "x" + resultHeight);
    System.out.println("PanelRatio: " + (float)resultWidth / resultHeight);
}