Thursday, 6 September 2012

Java Label


Introduction of Java Label


Label in java used for display static text on frame.We can use following method for this purpose.
  1. Constructors
    
    Label()
    Label(String matter);
    Label(String matter, int alignment)
    
    Value of alignment may be Label.LEFT|Label.RIGHT|Label.CENTER
    
    
  2. Text method
    void setText(String matter);
    String getText();
    
    
  3. Foreground color method
    
    void setForeground(Color);
    Color getForeground();
    
    
  4. Background color method
    
    void setBackground(Color);
    Color getBackground();
    
    
  5. Alignment method
    
    void setAlignment(int)
    int getAlignment()
    
    Value of alignment may be Label.LEFT|Label.RIGHT|Label.CENTER
    
    
  6. Location method
    
    void setLocation(int x, int y);
    void setLocation(Point);
    Point getLocation();
    
    
  7. Labe Size method
    
    void setSize(int width, int height);
    void setSize(Dimension);
    Dimension getSize();
    
Programm

Save MyFrameListener.java

import java.awt.event.*;
class MyFrameListener extends WindowAdapter
{
 public void windowClosing(WindowEvent we)
 {
  System.exit(0);
 }
}


Save MyFrame.java 
 
import java.awt.*;
class MyFrame extends Frame
{
 Label l1, l2, l3;
 
 MyFrame()
 {
  super("Sample Java Frame");
  MyWindowAdapter mwa = new MyWindowAdapter();
  addWindowListener(mwa);
  setSize(500,300);
  addControls();
  setVisible(true);
 }
 
 private void addControls()
 {
  int lblWidth, lblHeight, x, y; 
   
  x = 10;
  y = 50;
  lblWidth = 150;
  lblHeight = 20;
  
  setLayout(null);
  
  l1 = new Label();
  l2 = new Label("I am a sample Label");
  l3 = new Label("Yet one more label", Label.RIGHT);
  
  l1.setText("This is to display static text");
  
  l1.setBackground(Color.YELLOW);
  l2.setBackground(Color.GREEN);
  l3.setBackground(Color.GRAY);
  
  l2.setAlignment(Label.CENTER);
  
  l1.setForeground(Color.RED);
  
  
  l1.setSize(lblWidth, lblHeight);
  l2.setSize(lblWidth, lblHeight);
  l3.setSize(lblWidth, lblHeight);
  
  l1.setLocation(x,y);
  l2.setLocation(x,y+50);
  l3.setLocation(x,y+100);
    
  
  add(l1);
  add(l2);
  add(l3);
 }
}


Save GUI1.java

public class GUI1
{
 public static void main(String args[])
 {
  MyFrame mf = new MyFrame();
 }
}

No comments:

Post a Comment