我如何调整一个JFrame,同时保持长宽比?

时间:2022-08-27 11:03:08

I want the user to be able to resize the JFrame's width/height, while maintaining the same ratio between its width and height. In other words, I want to force the height to change with the width keeping the same frame shape.

我希望用户能够调整JFrame的宽度/高度,同时保持其宽度和高度的相同比例。换句话说,我想要使高度改变,宽度保持相同的框架形状。

public void componentResized(ComponentEvent arg0)
    {
        int setHeight = arg0.getComponent().getHeight();
        int setWidth = arg0.getComponent().getWidth();
        double newWidth = 0;
        double newHeight = 0;
        {
            if(setHeight != oldHeight)
            {
                heightChanged = true;
            }
            if(setWidth != oldWidth)
            {
                widthChanged = true;
            }
        }
        {
            if(widthChanged == true && heightChanged == false)
            {
                newWidth = setWidth;
                newHeight = setWidth*HEIGHT_RATIO;
            }
            else if(widthChanged == false && heightChanged == true)
            {
                newWidth = setHeight * WIDTH_RATIO;
                newHeight = setHeight;
            }
            else if(widthChanged == true && heightChanged == true)
            {
                newWidth = setWidth;
                newHeight = setWidth*HEIGHT_RATIO;
            }
        }

        int x1 = (int) newWidth;
        int y1 = (int) newHeight;
        System.out.println("W: " + x1 + " H: " + y1);
        Rectangle r = arg0.getComponent().getBounds();
        arg0.getComponent().setBounds(r.x, r.y, x1, y1);
        widthChanged = false;
        heightChanged = false;
        oldWidth = x1;
        oldHeight = y1;
    }

2 个解决方案

#1


4  

Have a look at J frame resizing in an aspect ratio

看一下J帧在长宽比中的调整?

I think this is what you need.

我想这就是你需要的。

@Override
public void componentResized(ComponentEvent arg0) {
    int W = 4;  
    int H = 3;  
    Rectangle b = arg0.getComponent().getBounds();
    arg0.getComponent().setBounds(b.x, b.y, b.width, b.width*H/W);

}

#2


1  

Try using a componentEvent to signal when the frame is resized. When the frame is resized keep track of which dimension (x or y) was changed. Use the getSize() method to get the frame dimensions and then get .getWidth() or .getHeight() to get the new size the frame was adjusted to. Use the new size of the changed dimension to calculate the desired size for the other dimension and set the size of the frame using .setSize(int width, int height).

尝试使用一个组件来在帧大小调整时发出信号。当帧被调整大小时,跟踪哪一个维度(x或y)被改变。使用getSize()方法获得框架维度,然后获取. getwidth()或. getheight()以获得新大小的帧。使用更改后的尺寸的新大小来计算其他尺寸所需的尺寸,并使用. setsize (int width, int height)设置框架的大小。

This might have problems if you change both the width and height in the same resizing, if so you can always base the height off the width or vice versa

如果你改变相同大小的宽度和高度,这可能会有问题,如果这样,你总是可以把高度从宽度上降低,反之亦然。

#1


4  

Have a look at J frame resizing in an aspect ratio

看一下J帧在长宽比中的调整?

I think this is what you need.

我想这就是你需要的。

@Override
public void componentResized(ComponentEvent arg0) {
    int W = 4;  
    int H = 3;  
    Rectangle b = arg0.getComponent().getBounds();
    arg0.getComponent().setBounds(b.x, b.y, b.width, b.width*H/W);

}

#2


1  

Try using a componentEvent to signal when the frame is resized. When the frame is resized keep track of which dimension (x or y) was changed. Use the getSize() method to get the frame dimensions and then get .getWidth() or .getHeight() to get the new size the frame was adjusted to. Use the new size of the changed dimension to calculate the desired size for the other dimension and set the size of the frame using .setSize(int width, int height).

尝试使用一个组件来在帧大小调整时发出信号。当帧被调整大小时,跟踪哪一个维度(x或y)被改变。使用getSize()方法获得框架维度,然后获取. getwidth()或. getheight()以获得新大小的帧。使用更改后的尺寸的新大小来计算其他尺寸所需的尺寸,并使用. setsize (int width, int height)设置框架的大小。

This might have problems if you change both the width and height in the same resizing, if so you can always base the height off the width or vice versa

如果你改变相同大小的宽度和高度,这可能会有问题,如果这样,你总是可以把高度从宽度上降低,反之亦然。