JFrame 仅可调整高度 [英] JFrame resizable height ONLY

查看:21
本文介绍了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.

推荐答案

下面的代码做的很好.

addComponentListener(new ComponentAdapter() {

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

});

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

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