Wednesday, August 10, 2011

JButton Borders

Output:
Code:

/**

* File: jbuttonBoders.java

* Tiltle: JButton Borders

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

*/



//Java Core Package

import javax.swing.*;

//Java Extension Package

import java.awt.*;



public class jbuttonBoders extends JFrame {



//Initializing JButton and JPanel

private JButton button[];

private JPanel panel;



//Setting up GUI

public jbuttonBoders() {



//Setting up the Title of the Window

super("JButton Border");



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

setSize(220,190);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JButton with an array of 12

button = new JButton[12];



//Constructing JPanel with a FlowLayout CENTER Position

panel = new JPanel();

panel.setLayout(new FlowLayout(FlowLayout.CENTER));



//Constructing all 12 JButtons using "for loop"

for(int count=0; count<button.length; count++) {

button[count] = new JButton("Button "+(count+1));

panel.add(button[count]);

}



//Setting Different Borders on each JButton

button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border

button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border

button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border

button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border

button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel

button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel

button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel

button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel

button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)

button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line

button[10].setBorder(BorderFactory.createEtchedBorder(1)); //

button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border



/** The Borders shown above are the basic borders that we commonly used.

* There are still lots of Border Styles available so all you have to do is to discover

* and have some experiment using all the available borders. I recommend you use JCreator Pro

* if want to know more about different border styles and learn how to implement them.

*/



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

Container pane = getContentPane();

setContentPane(pane);



//Adding the JPanel to the container

pane.add(panel);



/**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) {

jbuttonBoders jbb = new jbuttonBoders();

}

}


Important Part of the Program: 

//Setting Different Borders on each JButton

button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border

button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border

button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border

button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border

button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel

button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel

button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel

button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel

button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)

button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line

button[10].setBorder(BorderFactory.createEtchedBorder(1)); //

button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border

1 comment: