JLABELS
Java Swing Tutorial Explaining the JLabel Component. JLabel, descended from JComponent, is used to
create text labels.
A JLabel object provides text instructions or information on a GUI — display a single line of read-only text, an image or both text and image.
We use a Swing JLabel when we need a user interface component that displays a message or an image.
JØLabels
create text labels.
A JLabel object provides text instructions or information on a GUI — display a single line of read-only text, an image or both text and image.
We use a Swing JLabel when we need a user interface component that displays a message or an image.
JØLabels
- Provide text instructions on a GUI
- lRead-only text
- lPrograms rarely change a label’s contents
- lClass JLabel (subclass of JComponent)
JLABEL Example
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
public class JlabelDemo extends JPanel {
JLabel jlbLabel1, jlbLabel2, jlbLabel3;
public JlabelDemo() {
ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG",
"My Website");
// Creating an Icon
setLayout(new GridLayout(3, 1));
// 3 rows, 1 column Panel having Grid Layout
jlbLabel1 = new JLabel("Image with Text", icon, JLabel.CENTER);
// We can position of the text, relative to the icon:
jlbLabel1.setVerticalTextPosition(JLabel.BOTTOM);
jlbLabel1.setHorizontalTextPosition(JLabel.CENTER);
jlbLabel2 = new JLabel("Text Only Label");
jlbLabel3 = new JLabel(icon); // Label of Icon Only
// Add labels to the Panel
add(jlbLabel1);
add(jlbLabel2);
add(jlbLabel3);
}
public static void main(String[] args) {
JFrame frame = new JFrame("jLabel Usage Demo");
frame.addWindowListener(new WindowAdapter() {
// Shows code to Add Window Listener
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setContentPane(new JlabelDemo());
frame.pack();
frame.setVisible(true);
}
}
Output
Download jLabel Source Code
JAVA JLABEL HIERARCHY
javax.swing
Class JLabel
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JLabel
Class JLabel
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JLabel
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable, SwingConstants
Direct Known Subclasses:
BasicComboBoxRenderer, DefaultListCellRenderer, DefaultTableCellRenderer, DefaultTreeCellRenderer
Accessible, ImageObserver, MenuContainer, Serializable, SwingConstants
Direct Known Subclasses:
BasicComboBoxRenderer, DefaultListCellRenderer, DefaultTableCellRenderer, DefaultTreeCellRenderer
JLABEL CONSTRUCTOR
JLabel()
Creates a JLabel instance with no image and with an empty string for the title.
Creates a JLabel instance with no image and with an empty string for the title.
JLabel(Icon image)
Creates a JLabel instance with the specified image.
Creates a JLabel instance with the specified image.
JLabel(Icon image, int horizontalAlignment)
Creates a JLabel instance with the specified image and horizontal alignment.
Creates a JLabel instance with the specified image and horizontal alignment.
JLabel(String text)
Creates a JLabel instance with the specified text.
Creates a JLabel instance with the specified text.
JLabel(String text, Icon icon, int horizontalAlignment)
Creates a JLabel instance with the specified text, image, and horizontal alignment.
Creates a JLabel instance with the specified text, image, and horizontal alignment.
JLabel(String text, int horizontalAlignment)
Creates a JLabel instance with the specified text and horizontal alignment.
Creates a JLabel instance with the specified text and horizontal alignment.
No comments:
Post a Comment