Wednesday, August 10, 2011

Add Icon on JButton

Output:
Code:

/**

* File: jbuttonIcon.java

* Tiltle: Add Icon on JButton

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

*/



//Java Core Package

import javax.swing.*;

//Java Extension Package

import java.awt.*;



public class jbuttonIcon extends JFrame {



//Initializing JButton

private JButton button;



//Setting up GUI

public jbuttonIcon() {



//Setting up the Title of the Window

super("Add Icon on JButton");



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

setSize(250,80);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing Icon class for JButton

Icon ico = new ImageIcon("icon.png");



//Constructing JButton

button = new JButton("Add New User",ico);



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

Container pane = getContentPane();

setContentPane(pane);



//Setting up the container layout

FlowLayout flow = new FlowLayout(FlowLayout.CENTER);

pane.setLayout(flow);



//Adding the JButton component to the container

pane.add(button);



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

jbuttonIcon pjb = new jbuttonIcon();

}

}


Important Part of the Program:

//Constructing Icon class for JButton

Icon ico = new ImageIcon("icon.png");



//Constructing JButton

JButton button = new JButton("Add New User",ico);


Required Image(s):


No comments:

Post a Comment