• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing the Graphics Context (font) of JButton Text?  RSS feed

 
John Azzolina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. This is probably super basic, but I'm stumped:
I want to change the font on a button and I know that this will work:
JButton bob = new JButton( "Bob" );
{ bob.setFont( new Font ( "Serif", Font.BOLD, 20) ); }
what I want to know is what function do the braces play here?
Why do I need them?
Thanks in advance,
John Azzolina
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the braces. The code works fine without them.
 
John Azzolina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela -- thank you!
But, here's the thing (and I apologize for being vague earlier) if you use the bob.setFont( . . . ) outside of any method, it won't work without the braces. But it will with the braces. I have to be able to explain this to some other people and am concerned about why this works this way. Any help is appreciated!!
Thanks again,
John
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only code that is allowed outside of a method or code block is variable declarations. That is why your statement does not run without the braces. When you add the braces to your statement, you are creating what is called an instance initializer. This is code that will only be run one time, when your object is first created. See the following link to the Java language specs for more information:
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#246032
 
John Azzolina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much!! I knew it was something pretty simple, but could not find a reference to it anywhere. Thanks for your help!
John
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!