Discuss AWT components TextArea, Label briefly with example.
Label Class
Label 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.
Commonly used Constructors
public Label(String strLabel, int alignment);
public Label(String strLabel);
public Label();
Commonly used Public Methods
public String getText(); // returns text of label control
public void setText(String strLabel); //sets label text
public int getAlignment();//returns alignment of label text
public void setAlignment(int alignment); //sets alignment of label text
OR,
Java AWT Label
The object of the Label class is a component for placing text in a container. It is used to display a single line of read-only text. The text can be changed by a programmer but a user cannot edit it directly.
It is called a passive control as it does not create an event when it is accessed. To create a label, we need to create the object of the Label class.
AWT Label Class Declaration
public class Label extends Component implements Accessible
AWT Label Fields
The java.awt component class has the following fields:
- static int LEFT: It specifies that the label should be left justified.
- static int RIGHT: It specifies that the label should be right justified.
- static int CENTER: It specifies that the label should be placed in center.
Java AWT Label Example
In the following example, we are creating two labels l1 and l2 using the Label(String text) constructor and adding them into the frame.
LabelExample.java
import java.awt.*;
public class LabelExample {
public static void main(String args[]){
// creating the object of Frame class and Label class
Frame f = new Frame ("Label example");
Label l1, l2;
// initializing the labels
l1 = new Label ("First Label.");
l2 = new Label ("Second Label.");
// set the location of label
l1.setBounds(50, 100, 100, 30);
l2.setBounds(50, 150, 100, 30);
// adding labels to the frame
f.add(l1);
f.add(l2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
TextField Class
The TextField component allows the user to edit a single line of text. When the user types a key the text field the event is sent to the TextField. The key event may be key pressed, Key release or key typed. The key event is passed to the registered KeyListener. It is also possible to for ActionEvent if the ActionEvent is enabled on the text field.
Commonly used Constructors
public TextField(String strInitialText, int columns); // Construct a TextField instance with the given initial text string with the number of columns.
public TextField(String strInitialText); //Construct a TextField instance with the given initial text string.
public TextField(int columns); // Construct a TextField instance with the number of columns.
Commonly used Public Methods
public String getText(); // Get the current text on this TextField instance
public void setText(String strText);// Set the display text on this TextField instance
public void setEditable(boolean editable);//Set the textfield to editable (read/write) or non-editable (read-only)
OR,
Java AWT TextField
The object of a TextField class is a text component that allows a user to enter a single line of text and edit it. It inherits TextComponent class, which further inherits the Component class.
When we enter a key in the text field (like key pressed, key released or key typed), the event is sent to TextField. Then the KeyEvent is passed to the registered KeyListener. It can also be done using ActionEvent; if the ActionEvent is enabled on the text field, then the ActionEvent may be fired by pressing the return key. The event is handled by the ActionListener interface.
AWT TextField Class Declaration
public class TextField extends TextComponent
Example
// importing AWT class
import java.awt.*;
public class TextFieldExample1 {
// main method
public static void main(String args[]) {
// creating a frame
Frame f = new Frame("TextField Example");
// creating objects of textfield
TextField t1, t2;
// instantiating the textfield objects
// setting the location of those objects in the frame
t1 = new TextField("Welcome to Javatpoint.");
t1.setBounds(50, 100, 200, 30);
t2 = new TextField("AWT Tutorial");
t2.setBounds(50, 150, 200, 30);
// adding the components to frame
f.add(t1);
f.add(t2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Comments
Post a Comment