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

Java 最大化JFrame时调整JPanel的大小,java,resize,jframe,jpanel,maximize,Java,Resize,Jframe,Jpanel,Maximize,好的,下面的代码显示了程序第一次运行时JFrame中的JPanel。如果通过拖动框架的一个边或角来重新调整窗口的大小,JPanel将重新调整自身的大小并保持监视器的纵横比 注意:JPanel设置为仅在分辨率为1920x1080的监视器上保持在窗口范围内。在任何其他显示器尺寸上,JPanel可能会被切断。请参阅updatePanelSize()方法中setPreferredSize()上面的注释 我遇到的问题是,如果我通过双击窗口工具栏(或窗口顶部的正确术语)或单击最大化按钮来最大化窗口,JPan

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

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


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

在调整面板大小时,面板会立即接受
updatePanelSize()
中的新首选维度,但在最大化/还原时,面板显然会忽略新的首选维度

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);
}
我添加了对
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);
}

谢谢,我没有注意到
revalidate()
方法调用。