JFrame 仅可调整高度

2022-01-24 00:00:00 user-interface java swing jframe resizable

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);
    }

});

相关文章