Sunday, September 4, 2011

Basic JPanel Borders

Design Free Website with Free Internet Marketing Tools

Program Description:

The Java Program below is just a simple java code showing different JPanel borders. I have created 15 types of JPanel borders that is commonly used but there are more borders available using BorderFactory class, all you have to do is explore. The purpose of this program is to show how to use the class BorderFactory in creating different kinds of borders.

Output:
Code:

/**
 * File: jpanelBorders.java
 * Tiltle: Basic JPanel Borders
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class jpanelBorders extends JFrame {
	
	//Initializing JPanel
	private JPanel panels[];

	//Setting up GUI
    public jpanelBorders() {
    	
    	//Setting up the Title of the Window
    	super("Basic JPanel Borders");

    	//Set Size of the Window (WIDTH, HEIGHT)
    	setSize(600,300);

    	//Exit Property of the Window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    	//Constructing JPanel
    	panels = new JPanel[15];

    	//Setting up the container ready for the components to be added.
    	Container pane = getContentPane();
    	setContentPane(pane);

    	//Setting up the container layout
    	GridLayout grid = new GridLayout(3,5);
    	pane.setLayout(grid);
    	
    	//Constructing 15 JPanels using "for loop" and add automatically in the container
    	for(int count = 0; count<panels.length; count++) {
    		panels[count] = new JPanel();
    		pane.add(panels[count]);
    	}
    	
    	//Beveled Borders
    	panels[0].setBorder(BorderFactory.createBevelBorder(0));
    	panels[1].setBorder(BorderFactory.createBevelBorder(1));
    	panels[2].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green));
    	panels[3].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green));
    	panels[4].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green, Color.blue, Color.cyan));
    	panels[5].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green, Color.blue, Color.cyan));
    	
    	//Line Borders
    	panels[6].setBorder(BorderFactory.createLineBorder(Color.blue));
    	panels[7].setBorder(BorderFactory.createLineBorder(Color.blue,10));
    	
    	//Titled Border
    	panels[8].setBorder(BorderFactory.createTitledBorder("String Border"));
    	panels[9].setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(1), "Custom SB"));
    	
    	//Etched Border
    	panels[10].setBorder(BorderFactory.createEtchedBorder(0));
    	panels[11].setBorder(BorderFactory.createEtchedBorder(1));
    	panels[12].setBorder(BorderFactory.createEtchedBorder(Color.red, Color.blue));
    	panels[13].setBorder(BorderFactory.createEtchedBorder(0, Color.black, Color.cyan));
    	panels[14].setBorder(BorderFactory.createEtchedBorder(1, Color.black, Color.cyan));

    	/**Set all the Components Visible.
    	 * If it is set to "false", the components in the container will not be visible.
    	 */
    	setVisible(true);
    }
    
	//Main Method
    public static void main (String[] args) {
    	jpanelBorders jpb = new jpanelBorders();
	}
}

Important Part of the Program:

//Constructing 15 JPanels using "for loop" and add automatically in the container
    	for(int count = 0; count<panels.length; count++) {
    		panels[count] = new JPanel();
    		pane.add(panels[count]);
    	}
    	
    	//Beveled Borders
    	panels[0].setBorder(BorderFactory.createBevelBorder(0));
    	panels[1].setBorder(BorderFactory.createBevelBorder(1));
    	panels[2].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green));
    	panels[3].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green));
    	panels[4].setBorder(BorderFactory.createBevelBorder(0, Color.red, Color.green, Color.blue, Color.cyan));
    	panels[5].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.green, Color.blue, Color.cyan));
    	
    	//Line Borders
    	panels[6].setBorder(BorderFactory.createLineBorder(Color.blue));
    	panels[7].setBorder(BorderFactory.createLineBorder(Color.blue,10));
    	
    	//Titled Border
    	panels[8].setBorder(BorderFactory.createTitledBorder("String Border"));
    	panels[9].setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(1), "Custom SB"));
    	
    	//Etched Border
    	panels[10].setBorder(BorderFactory.createEtchedBorder(0));
    	panels[11].setBorder(BorderFactory.createEtchedBorder(1));
    	panels[12].setBorder(BorderFactory.createEtchedBorder(Color.red, Color.blue));
    	panels[13].setBorder(BorderFactory.createEtchedBorder(0, Color.black, Color.cyan));
    	panels[14].setBorder(BorderFactory.createEtchedBorder(1, Color.black, Color.cyan));

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code