P. 1
OOP2 with JAVA

OOP2 with JAVA


|Views: 1,640|Likes:
Published by semillastan

More info:

Published by: semillastan on Jul 09, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





JLabel is certainly the simplest type of component. An object of type JLabel exists
just to display a line of text. The text cannot be edited by the user, although it can
be changed by your program. The constructor for a JLabel specifies the text to be

JLabel message = new JLabel(" Hello World! ");

There is another constructor that specifies where in the label the text is located, if
thereisextraspace. ThepossiblealignmentsaregivenbytheconstantsJLabel.LEFT,
JLabel.CENTER, and JLabel.RIGHT. For example,

JLabel message = new JLabel(" Hello World! ", JLabel.CENTER);

creates a label whose text is centered in the available space. You can change the text
displayed in a label by calling the label’s setText() method:

message.setText("Goodby World! ");

Since JLabel is a subclass of JComponent, you can use JComponent methods such
as setForeground() with labels. If you want the background color to have any effect,
call setOpaque(true) on the label, since otherwise the JLabel might not fill in its
background. For example:

JLabel message = new JLabel(" Hello World! ", JLabel.CENTER);
message.setForeground(Color.red); // Display red text . . .
message.setBackground(Color.black); // on a black background . . .
message.setFont(new Font(" Serif ",Font.BOLD,18)); // in a big bold font .
message.setOpaque(true); // Make sure background is filled in .

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->