Saturday, August 27, 2011

JLabel Positions

Program Description:

The Java program below is a JLabel demonstration that shows different JLabel positions.

Output:
Code:

/**

* File: jlablePositions.java

* Tiltle: JLabel Positions

* Author: http://java-code-complete.blogspot.com/

*/



//Java Extension Packages

import javax.swing.*;

//Java Core Packages

import java.awt.*;



public class jlablePositions extends JFrame {



//Setting up GUI

public jlablePositions() {



//Setting up the Title of the Window

super("JLabel Positions");



//Set Size of the Window (WIDTH, HEIGHT)

setSize(350,200);



//Exit Property of the Frame of Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JLabel with different positions

JLabel trailing = new JLabel("TRAILING POSITION",JLabel.TRAILING);

JLabel left = new JLabel("LEFT POSITION",JLabel.LEFT);

JLabel right = new JLabel("RIGHT POSITION",JLabel.RIGHT);

JLabel center = new JLabel("CENTER POSITION",JLabel.CENTER);

JLabel leading = new JLabel("LEADING POSITION",JLabel.LEADING);



//Setting up the container ready for the components to be added

Container pane = getContentPane();



//Setting the position of the JLabel

GridLayout flo = new GridLayout(5,1);



//Adding the Layout and JLabel in the container

pane.setLayout(flo);

pane.add(trailing);

pane.add(left);

pane.add(right);

pane.add(center);

pane.add(leading);

setContentPane(pane);

setVisible(true);

}



//Main Method

public static void main(String[] args) {

jlablePositions jls = new jlablePositions();

}

}


Important Part of the Program:

//Constructing JLabel with different positions

JLabel trailing = new JLabel("TRAILING POSITION",JLabel.TRAILING);

JLabel left = new JLabel("LEFT POSITION",JLabel.LEFT);

JLabel right = new JLabel("RIGHT POSITION",JLabel.RIGHT);

JLabel center = new JLabel("CENTER POSITION",JLabel.CENTER);

JLabel leading = new JLabel("LEADING POSITION",JLabel.LEADING);

No comments:

Post a Comment