我正在创建一个应用程序,我相信如果它在扩展/最小化时能够保持其纵横比,效果会更好。我将最小尺寸设置为 1000x800,并希望它保持 5:4 的比例。
是否已经有一种方法可以实现这一点,或者我需要自己添加一个事件处理程序来检查框架是否已调整大小,是否会为其提供尽可能最接近的大小?
PS.如果问题重复,抱歉,我找不到它
编辑
感谢所有帮助我完成这件事的人。以下是我如何保持应用程序的大小以满足正在使用的屏幕的大小,而不会太大或太小并保持纵横比。 干杯
int width = frame.getBounds().width;
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (width > gd.getDisplayMode().getWidth())
width = gd.getDisplayMode().getWidth();
while (width*4/5 > gd.getDisplayMode().getHeight())
width = (int) (width - width*0.1);
frame.setBounds(frame.getBounds().x, frame.getBounds().y, width, width*4/5);
最佳答案
试试这个:
@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);
}
关于java - 如何保持 JFrame 的宽高比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44490655/