Pin It

How to add label to a Swing frame in Java ?

Program to demonstrate adding of a Label to a Swing component in Java.

package com.hubberspot.awtSwing.example;

import java.awt.*;
import javax.swing.*;

public class LabelDemo
{
  JFrame frame;
  JLabel label1, label2, label3, label4;
  LabelDemo()
  {
    frame = new JFrame();
    frame.setLayout(new GridLayout(4,1));
        
    label1 = new JLabel(" Left Label "); 
    label2 = new JLabel(" Center Label ",
         JLabel.CENTER);
    
    label3 = new JLabel(" Right Label",
         JLabel.RIGHT); 
    
    label4 = new JLabel(" Image Label", 
        new ImageIcon("c:\\smile.jpg"),JLabel.CENTER);
    
    frame.add(label1); 
    frame.add(label2); 
    frame.add(label3); 
    frame.add(label4);
    frame.setTitle("Welcome to Hubberspot!.");
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
   public static void main(String[] args)
   {
     new LabelDemo();
   }
}


Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot