Question

i'm working with layout set to null, i have a JFrame with an aspect ratio and during the resize, this frame keep the same aspect ratio.

Every time i resize the frame i resize all my components without any problems just one with JLabel.

When i resize the bounds of a JLabel the text don't increase to fit the new bounds, the text stay at the same size.

I'm trying to solve the problem by setting every time the font size but it seems to be difficult.

Is there a way to achieve that ?

Was it helpful?

Solution

Have you tried working with a particular layout that lets you maintain the aspect ratio? Such as GridbagLayout? Use an appropriate Layout such as GridBagLayout or SpringLayout

example: Container contentPane = jframe.getContentPane(); contentPane.setLayout ( new GridBagLayout() );

// x y w h wtx wty anchor fill T L B R padx pady contentPane.add( myPanel, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 10, 10, 10, 5 ), 0, 0 ) ); contentPane.add( myPanel2, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets( 10, 5, 10, 10 ), 0, 0 ) );

You also would like labels, etc. to be expanded as well. Hmm, I don't know of anything that will do this automatically for you. You could extend some of the components to take hints from the layout and re size appropriately. For instance, you may choose to enlarge the font when you determine more white space is available due to a window re size. I'm not aware of existing component that will do this for labels. Proportional fonts are a bit tricky to get this right. Actually, if memory serves me, I once did something similar. It was like "trial-and-error" pro-grammatically. Off screen, I would draw the character string in the estimated font size and redraw down until it fit in the allotted space with the desired margins. Sure seems like a lot of trouble to get these dynamic labels to look good!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top