Introduction
The label class is a passive control because it does not create any event when accessed by the user. The label control is an object of Label. A label displays a single line of read-only text. However the text can be changed by the application programmer but cannot be changed by the end user in any way.
Class declaration
Following is the declaration for java.awt.Label class:
public class Label extends Component implements Accessible
Field
Following are the fields for java.awt.Component class:
- static int CENTER — Indicates that the label should be centered.
- static int LEFT — Indicates that the label should be left justified.
- And static int RIGHT — Indicates that the label should be right justified.
Class constructors
S.N. | Constructor & Description |
---|---|
1 | Label()Constructs an empty label. |
2 | Label(String text)Constructs a new label with the specified string of text, left justified. |
3 | Label(String text, int alignment)Constructs a new label that presents the specified string of text with the specified alignment. |
Class methods
S.N. | Method & Description |
---|---|
1 | void addNotify()Creates the peer for this label. |
2 | AccessibleContext getAccessibleContext()Gets the AccessibleContext associated with this Label. |
3 | int getAlignment()Gets the current alignment of this label. |
4 | String getText()Gets the text of this label. |
5 | protected String paramString()Returns a string representing the state of this Label. |
6 | void setAlignment(int alignment)Sets the alignment for this label to the specified alignment. |
7 | void setText(String text)Sets the text for this label to the specified text. |
Methods inherited
This class inherits methods from the following classes:
- java.awt.Component
- java.lang.Object
Label Example
Create the following java program using any editor of your choice in say D:/ > AWT > com > adglob > gui >AwtControlDemo.java
package com.adglob.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showLabelDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showLabelDemo(){ headerLabel.setText("Control in action: Label"); Label label = new Label(); label.setText("Welcome to adglob AWT Tutorial."); label.setAlignment(Label.CENTER); label.setBackground(Color.GRAY); label.setForeground(Color.WHITE); controlPanel.add(label); mainFrame.setVisible(true); } }
Compile the program using the command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\adglob\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using the following command.
D:\AWT>java com.adglob.gui.AwtControlDemo
Previous Topic:-Click Here
Pingback: AWT Controls - Adglob Infosystem Pvt Ltd
Pingback: Bangkok massage outcall
Pingback: ν°λΉμν€