JFrame resizable height ONLY

后端 未结 7 1479
我在风中等你
我在风中等你 2020-12-15 13:16

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?

7条回答
  •  爱一瞬间的悲伤
    2020-12-15 13:30

    I do not think there is a method expressly for that purpose. However, you could preset the JFrame's preferred, minimum, and maximum size such that the widths are all equal.

    Dimension dimPreferred = frame.getPreferedSize();
    Dimension dimMinimum = frame.getMinimumSize();
    Dimension dimMaximum = frame.getMaximumSize();
    dimPreferred.setWidth( FIXED_WIDTH );
    dimMinimum.setWidth( FIXED_WIDTH );
    dimMaximum.setWidth( FIXED_WIDTH );
    frame.setPreferredSize( dimPreferred );
    frame.setMinimumSize( dimMinimum );
    frame.setMaximumSize( dimMaximum );
    

    You will probably want to do this after frame.pack() and before frame.setVisible(true).

提交回复
热议问题