***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***

posts

    Java AWT Label

    Label 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 any event when it is accessed. To create a label, we need to create the object of Label class.

    public class Label extends Component implements Accessible  

    Label class Constructors:

    1.   Label()                          - It constructs an empty label.

    2.   Label(String text)         - It constructs a label with the given string (left justified by default).

    3.   Label(String text, int alignement)             - It constructs a label with the specified string and the specified alignment.

    Label Class Methods:

    1.    void setText(String text)                - It sets the texts for label with the specified text.

    2.    void setAlignment(int alignment)   - It sets the alignment for label with the specified alignment.

    3.    String getText()                              - It gets the text of the label

    4.    int getAlignment()                          - It gets the current alignment of the label.

    5.    void addNotify()                            - It creates the peer for the label.

    6.    AccessibleContext getAccessibleContext()         - It gets the Accessible Context associated with the label.

    7.    protected String paramString()       - It returns the string the state of the label.

    Ex:

    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 ("Ashraf.");  

        l2 = new Label ("Mohammad.");  

         // 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);   

    }   

    }

    No comments:

    Post a Comment