Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Q: how to put a JPEG to the north region (of the frame's contentPane)  RSS feed

 
G�rkan G�nay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner and reading �Head First Java 2nd Edition�.
I have finished the GUI Chapter and wanted to program some GUI Stuff, but there is one problem:

I�m overriding the paintComponent method:



and using it:



The Image (600x80) is not fully displayed. The width is O.K. but the height is only some Pixels (may be 5 or so).

It will be fully displayed when I put it to the center region:


� but I don�t want to have it in the center region, because center I do need for some other stuff.

I would be very happy if somebody could help me ;-)
[ July 19, 2007: Message edited by: G�rkan G�nay ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unless you plan to do something else with LogoPanel, you don't need it.

change
frame.getContentPane().add(BorderLayout.NORTH, logo);
to
frame.getContentPane().add(BorderLayout.NORTH, new JLabel(new ImageIcon("logo.jpg"),JLabel.LEFT));

assumes you want the logo on the left (from drawImage(0,0)).
If not, modify JLabel.LEFT, to suit

[EDIT]
just reread your post, and see you are experimenting, so I haven't actually
answered your query.

when you put something in BorderLayout.NORTH (or any layoutManager 'position'),
the layoutManager uses the preferredSize of the component for sizing. Because
you have only drawn an image, and not given the panel a size, the layoutManager
in this instance (BorderLayout) uses the width of the frame (it is specified),
but the height is almost 0 (nothing is specified). It works in CENTER because CENTER
gets all remaining space. the apidocs for BorderLayout will
define it better than I can.

here's your code modified with some comments - see if you can follow the changes


[ July 16, 2007: Message edited by: Michael Dunn ]
 
G�rkan G�nay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOW ! what a nice answer !
It works now and I haved learned a lot
Thank you very much.

P.S. This board seems to be a very cool one
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!