JLabel in more detail. JLabel
You can get the program here.
1:import javax.swing.*; 2:import java.awt.*; 3: 4:public class HelloKittyFrame extends JFrame { 5: // 2-dimensional array 6: private JLabel HelloKittyLabels[][] = new JLabel[3][3]; 7: // file name of the image 8: private String imageFileName = "hellokitty.gif"; 9: // text to be displayed on labels 10: private String text = "Hello Kitty"; 11: // icon 12: private ImageIcon icon = new ImageIcon(imageFileName); 13: private Container contentPane = getContentPane(); 14: public HelloKittyFrame() { 15: super("Hello Kitty"); 16: int row = 3, col = 3; 17: contentPane.setLayout(new GridLayout(row, col)); 18: // initiate the labels and add to the frame 19: for(int i = 0; i < 3; i++) { 20: for(int j = 0; j < 3; j++) { 21: HelloKittyLabels[i][j] = new JLabel(text, icon, JLabel.CENTER); 22: contentPane.add(HelloKittyLabels[i][j]); 23: } 24: } 25: // set horizontal text position and vertical text position 26: HelloKittyLabels[0][0].setHorizontalTextPosition(JLabel.LEFT); 27: HelloKittyLabels[0][0].setVerticalTextPosition(JLabel.TOP); 28: HelloKittyLabels[0][1].setHorizontalTextPosition(JLabel.CENTER); 29: HelloKittyLabels[0][1].setVerticalTextPosition(JLabel.TOP); 30: HelloKittyLabels[0][2].setHorizontalTextPosition(JLabel.RIGHT); 31: HelloKittyLabels[0][2].setVerticalTextPosition(JLabel.TOP); 32: HelloKittyLabels[1][0].setHorizontalTextPosition(JLabel.LEFT); 33: HelloKittyLabels[1][0].setVerticalTextPosition(JLabel.CENTER); 34: HelloKittyLabels[1][1].setHorizontalTextPosition(JLabel.CENTER); 35: HelloKittyLabels[1][1].setVerticalTextPosition(JLabel.CENTER); 36: HelloKittyLabels[1][2].setHorizontalTextPosition(JLabel.RIGHT); 37: HelloKittyLabels[1][2].setVerticalTextPosition(JLabel.CENTER); 38: HelloKittyLabels[2][0].setHorizontalTextPosition(JLabel.LEFT); 39: HelloKittyLabels[2][0].setVerticalTextPosition(JLabel.BOTTOM); 40: HelloKittyLabels[2][1].setHorizontalTextPosition(JLabel.CENTER); 41: HelloKittyLabels[2][1].setVerticalTextPosition(JLabel.BOTTOM); 42: HelloKittyLabels[2][2].setHorizontalTextPosition(JLabel.RIGHT); 43: HelloKittyLabels[2][2].setVerticalTextPosition(JLabel.BOTTOM); 44: } 45: 46: public static void main(String[] args) { 47: HelloKittyFrame f = new HelloKittyFrame(); 48: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 49: f.setSize(600,500); 50: f.setVisible(true); 51: } 52:}
ImageIcon is a class in package javax.swing. It
represents an icon. It make an icon from an image file such as
x.gif or y.jpg . One constructor is
Here theImageIcon(String filename)
filename is the file name of your image.The meaning of the constructors should be clear.JLabel() JLabel(Icon icon) JLabel(Icon icon, int horizontalAlignment) JLabel(String text) JLabel(String text, Icon icon, int horizontalAlignment) JLabel(String text, int horizontalAlignment)
text is the text displayed on the label.
icon is the image.
horizontalAlignment is the alignment of the content.
setVerticalTextPosition(int) to set the position of the text
relative to the image. The value can be
JLabel.TOP, JLabel.CENTER, JLabel.BOTTOM.setHorizontalTextPosition(int) to set the position
fo the text relative to the image. The value can be
JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT.
The following table summarize the useful methods of JLabel:
| Method or Constructor | Purpose |
|---|---|
JLabel(Icon)
|
Create a JLabel instance,
initializing it to have the specified text/image/alignment.
The int argument
specifies the horizontal alignment
of the label's contents within its drawing area.
The horizontal alignment must be one of the following constants
LEFT, CENTER, RIGHT,
LEADING, or TRAILING.
|
void setText(String)
|
Set or get the text displayed by the label. |
void setIcon(Icon)
|
Set or get the image displayed by the label. |
| Method | Purpose |
|---|---|
void setHorizontalAlignment(int)
|
Set or get where in the label its contents should be placed.
The possible values can be
LEFT (the default for image-only labels),
CENTER (the default for text-only labels),
RIGHT,
LEADING, and
TRAILING.
For vertical alignment:
TOP,
CENTER (the default), and
BOTTOM.
|
void setHorizontalTextPosition(int)
|
Set or get where the button's text should be placed,
relative to the button's image.
The possible values
for horizontal position:
LEFT,
CENTER, and
RIGHT (the default).
For vertical position:
TOP,
CENTER (the default), and
BOTTOM.
|
void setIconTextGap(int)
|
Set or get the number of pixels between the label's text and its image. |
![]() Homer |
![]() Marge |
![]() Bart |
![]() Lisa |
![]() Maggie |
SimpsonLabel. It extends JLabel.
It has a constructor SimpsonLabel(String name) which creates a
label with the corresponding image at the center.
1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class SimpsonLabel extends JLabel { 5: private String name; // Homer, Marge, Bart, Lisa, Maggie, 6: private String[] nameList = {"Homer", "Marge", "Bart", "Lisa", "Maggie"}; 7: // the file names of the images 8: private String[] filenames = {"homer.gif", "marge.gif", "bart.gif", 9: "lisa.gif", "maggie.gif"}; 10: private Icon image; 11: public SimpsonLabel(String name) { 12: this.name = name; 13: setUpImage(); 14: } 15: 16: // set up the image 17: private void setUpImage() { 18: for(int i = 0; i < nameList.length; i++) { 19: if(name.equals(nameList[i])) { // if name is found 20: image = new ImageIcon(filenames[i]); // set the image 21: this.setIcon(image); // set the icon of the Label 22: this.setHorizontalAlignment(JLabel.CENTER); 23: return; // stop the method 24: } 25: } 26: // no match is found 27: this.setText("Unknown"); 28: } 29:}
JLabel. So SimpsonLabel
is a JLabel. super() from
JLabel(). this. It is not necessary to use this. I just
try to make it clear to you that I use methods inherited from JLabel.
. 1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class SimpsonTest { 5: public static void main(String[] args) { 6: JFrame f = new JFrame("Simpson"); 7: f.getContentPane().setLayout(new GridLayout(1,0)); 8: String[] name = {"Homer", "Lisa", "Maggie", "Bart", "Marge"}; 9: for(int i = 0; i < name.length; i++) { 10: f.getContentPane().add(new SimpsonLabel(name[i])); 11: } 12: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 13: f.setSize(250,70); 14: f.setVisible(true); 15: } 16:}