JFrame仅适用于可调整大小的高度 [英] JFrame resizable height ONLY

查看:402
本文介绍了JFrame仅适用于可调整大小的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JFrame.setResizable(true)允许用户调整窗口的宽度和高度。是否存在允许用户仅调整高度的方法?

JFrame.setResizable(true) lets the user resize both the width and height of a window. Does a method exist which allows the user to ONLY resize the height?

谢谢。

编辑:以下解决方案似乎不起作用。在360x600 JFrame上,

The solutions below do NOT seem to work. On a 360x600 JFrame,

setResizable(true);
pack();
setMaximizedBounds(new java.awt.Rectangle(0, 0, 360, 1200));
setMaximumSize(new java.awt.Dimension(360, 1200));
setMinimumSize(new java.awt.Dimension(360, 600));
setPreferredSize(new java.awt.Dimension(360, 600));
setVisible(true);

仍允许完全拉伸JFrame的宽度,并设置 setResizable(false )不允许任何事情被拉伸。

Still allows fully stretching the width of the JFrame, and setting setResizable(false) allows nothing to be stretched.

推荐答案

下面的代码可以正常工作。

The code below does the job right.

addComponentListener(new ComponentAdapter() {

    @Override
    public void componentResized(ComponentEvent e) {
        setSize(new Dimension(preferredWidth, getHeight()));
        super.componentResized(e);
    }

});

这篇关于JFrame仅适用于可调整大小的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆