How to make it so that when resizing the window it retains its proportions of height to width?

    1 answer 1

    It is necessary to add an observver of the corresponding event, the simplest version would look something like this:

    public class SynchronizedBoundsFrame extends JFrame { public SynchronizedBoundsFrame () { addComponentListener ( getBoundsListener () ); } public static void main ( final String [] args ) { final JFrame jFrame = new SynchronizedBoundsFrame (); jFrame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); jFrame.setSize ( 300, 200 ); jFrame.setVisible ( true ); } private ComponentListener getBoundsListener () { return new ComponentAdapter () { @Override public void componentResized ( final ComponentEvent e ) { // get component final Component component = e.getComponent (); final Rectangle bounds = component.getBounds (); bounds.height = bounds.width; component.setBounds ( bounds ); } }; } } 

    By and large, you need to declare such a component a listener in a separate class, in the constructor it should be asked what to synchronize and what is the ratio. It will work for any component.