Proportionally resizing JFrame

I'm trying to make the JFrame re-meaningful in an unusual way: the ratio of width to frame height should be constant. I wrote this simple code; in this case, the ratio is 1/2:

 public class Panel extends JFrame { public Panel() { addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { System.out.println("Reseized"); setSize(getSize().width, getSize().width * 2); } }); } 

But this frame has problems with redrawing, and it only works when the user drags the mouse. When the drag and drop is completed, the lower right corner of the frame is at the point where the user released the mouse.

Maybe the componentResized method is not applicable in this case? So what should I use? Thanks in advance.

+6
source share
2 answers

Window resizing is handled by the OS; you receive updates only after the fact. Therefore, although you can correct the size after resizing, you cannot limit the ratio when resizing.

+4
source

You may try

 ((JFrame)e.getTopLevelAncestor()).pack(); 

where e is the component. Let me know if this work

0
source

Source: https://habr.com/ru/post/899958/


All Articles