0

I have a resizable JFrame and (since I do not know much about Layouts yet) set its JPanel Layout to null.

Is there a way I can tell my program to resize each of its components' size relative to the window's size?

e.g. I have a 200x100 default sized JFrame, on which a button is 20x10. If I resize the window to 400x200, I want the button to be (still on the same position relative to the window's edges) resized to 40x20.

If that works is there a way to do the same thing with fonts? E.g. size 11 on 200x100 but something like size 14 on 400x200.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Skilly
  • 55
  • 1
  • 7
  • *"..I do not know much about Layouts yet) set its JPanel Layout to null."* Now is the time to learn them. Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). *"is there a way to do the same thing with fonts?"* Not easily, but here is an [example](http://stackoverflow.com/a/21066065/418556). – Andrew Thompson Mar 19 '15 at 00:48

2 Answers2

1

Regardless of what convention you use to scale your components and font, it is not a good idea to set a layout to null. Widgets behaving strangely when using "setlayout(null)" will give insight to why this is error prone. Automatically size JPanel inside JFrame might help with scaling when you change the size of the JFrame manually.

Community
  • 1
  • 1
pietas
  • 64
  • 5
0

Add a ComponentListener to your frame and create a table with the defaultSizes of the Components for a specific windowsize. The componentResized(ComponentEvent e) will be called each time the window is resized. Inside it, you can resize the components. Or you implement your own LayoutManager.