• Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie GUI Look Problems  RSS feed

 
Jonathan Sternberg
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to learn how to create a good looking user interface in Java so I can create simple programs such as a calculator. My GUI's always look like crap though. I'm trying to follow this tutorial (http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html) on the Java Sun site. My only problem is I copy and paste their exact code, and I get a different looking GUI than the picture that they have on their site. Are they doing anything special to make it look like that? For me, it's just a very small box that says, "Hello World" and the title of the Frame is cut off to say, "Hell..." at the top of a very small window. Thank you for any help.
 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weird/bad layouts of components in Swing apps is (in my experience) typically due to a lack of understanding the layout manager you're using, and the way that JFrame/JDialog & JPanel work.

Are you running Linux, Mac, or Windows?
[ June 19, 2008: Message edited by: Ted Smyth ]
 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jonathansternberg:
I am attempting to learn how to create a good looking user interface in Java so I can create simple programs such as a calculator. My GUI's always look like crap though. I'm trying to follow this tutorial (http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html) on the Java Sun site. My only problem is I copy and paste their exact code, and I get a different looking GUI than the picture that they have on their site. Are they doing anything special to make it look like that? For me, it's just a very small box that says, "Hello World" and the title of the Frame is cut off to say, "Hell..." at the top of a very small window. Thank you for any help.


I just ran the test program from the link you posted, and since you call pack(), the Frame is resized to fit the contents. The content is simply a very small JLabel, so the window tries to get very very small. To prevent this, you could not call pack, and instead set the size manually:




EDIT: also, to get the Minimize, Maximize, and Close buttons on the Frame to match the Look & Feel (the default is the Java Metal Look & Feel, but the Frame is decorated with the native OS buttons and looks), call JFrame.setDefaultLookAndFeelDecorated(true); before you create your JFrame.
[ June 19, 2008: Message edited by: Ted Smyth ]
 
Jonathan Sternberg
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running Windows, and have run the program using both Eclipse and the NetBeans IDE.

If pack is the problem, why does every single tutorial on the site tell me to call pack? If they're calling pack, how are they getting their window to look specifically like it does? Also, for future reference, is there any way for Java to get the monitor resolution? I want to make the program compatible for any resolution, but higher resolutions will have the objects as smaller than they would appear on my computer. Lower resolutions may have the object too big.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have to set preferred sizes on the included ("child") components to get pack() to show a sensible size.

As for screen resolution, it was discussed very recently: I think it was here.
 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jonathan Sternberg:
I am running Windows, and have run the program using both Eclipse and the NetBeans IDE.

If pack is the problem, why does every single tutorial on the site tell me to call pack? If they're calling pack, how are they getting their window to look specifically like it does? Also, for future reference, is there any way for Java to get the monitor resolution? I want to make the program compatible for any resolution, but higher resolutions will have the objects as smaller than they would appear on my computer. Lower resolutions may have the object too big.


pack() isn't a problem, per se. It just causes the frame to reduce to it's preferred size. If you jam in a bunch of components, including a table, some labels, textfields, combo boxes, etc using a layout manager, you'll find that pack() will do its best to respect the preferred sizes of all the components in the frame. The fact is that a JLabel containing "Hello World!" doesn't occupy much space, and it's minimum, preferred, and maximum sizes are pretty well set.

Try commenting out pack() and see what happens!

Resolution causing components to appear smaller or larger is a natural effect of changing resolutions... What is your main concern? Most people don't run resolutions lower than 1024x768 by this point, and I think that's a reasonable amount of space to work with and ensure a visual layout that can scale reasonably well.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!