• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI Size

 
Daniel Hoyt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still working on the same program as I was for my last question when I had located the string in the wrong part of the program. My other problem is the size of my GUI is off. First, I tried to use the pack(); code and when I do and run the program it ends up as a minimized window basically. I tried to do setSize and it seems to jumble up all the components until I maximize the window, then minimize the window, then somehow everything appears in place. Any advice on how I can size this thing correctly??
I appreciate the help I've found here and thank you for your time.

 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have broken one long line because it made the code hard to read.
I can never seem to get pack() to work myself. Maybe somebody will explain it; maybe you need to set a preferred size on each child component before pack() works.
If you are using grid bag, I suggest you comment out as many of the add instructions as you can. Get it down to one component only. Then compile and run the code. Then reinstate the second component (etc etc). Also, I suggest you get rid of grid bag constraints and use Horstmann's GBC class instead. If you can, find the description of GBC in Horstmann‘s Core Java II book (Vol I).

Another simpler solution (which I should have noticed earlier ‍) might be to move the setVisible() call to where it belongs. Only set anything visible after adding all its components. So setVisible should be the last line in the constructor/setUpGUI method.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something very non‑object‑oriented about your radio button listener. Create a class which has text as a field, and then you can get rid of those dreadful if‑elses.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The pack() method traverses the component tree working out the layout based on the preferred child component sizes and the layout managers being used. You should not set the frame visible until you have packed the component tree i.e. at the end of the construction of the GUI. Add at the end


of course after removing the frame.setVisible() and frame.setSize() calls at the start.


P.S. Why does your 'test' class extend JFrame but then use a separate JFrame for the inside the constructor? There is almost never any need to make your main class extend JFrame and in your case it is most definitely not needed.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!