Java Reference
In-Depth Information
Values are both equal to 0
Values are both equal to 37
Values are both equal to 16
Values are both equal to 0
Test passed.
3.3
The counter view
To implement the graphical part of the counter application we use two new graph-
ical components from the Swing library, labels and buttons .
3.3.1
Labels
A label is a rectangular component which displays text that cannot be edited by
the user (but might be changed by the program). Class JLabel realizes labels in
Swing. Here we present a constructor and a few methods.
public JLabel(String text)
public JLabel(ImageIcon picture)
public String getText()
public void setText(String text)
public void setText(String text, int alignment)
public void setForeground(Color c)
public void setBackground(Color c)
public void setOpaque( boolean b)
JLabel(String text) constructs a label which displays the text .
JLabel(ImageIcon picture) constructs a label which displays the image pic-
ture . For details on using images see Chapter 15.
getText() returns the text currently displayed in the label as a String .
setText(String text) replaces the text currently displayed in the label by text .
The new text is instantly displayed in the label.
setText(String text, int alignment) replaces the text currently displayed
in the label by text .Italso sets the alignment of the text, left, right or centre.
The possible values of alignment can be found in the class SwingConstants ,
e.g. use SwingConstants.CENTER to centre the text in the label.
setForeground(Color c) sets the text colour to c .
setBackground(Color c) sets the background colour to c . Note that labels are
!
transparent by default and their background colour is not visible. One sees
the background colour of the parent component shining through. To change the
Search WWH ::




Custom Search