Home - Programming - Java- Adding a Button to a Gui Window

Simple Gui Button Example

/**
 *	Application: Gui Button Example
 *	Author: Brian SonniE
 *	5/11/09
 *   www.fluidcoding.com - Coding Simplified.
 */
 
import javax.swing.*;
 
public class ButtonExample extends JFrame{
    private JButton myButton;
    private JPanel pane;
    
    public ButtonExample()
    {
  		// Make application close when exit is clicked
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	// Set the title on the top of the window
    	setTitle("Button Example");
    	// make it visible
    	setVisible(true);
    	// set the size (Width, Height) of window
    	setSize(400,100);
		// Build the JPanel
		buildPane();
    	// Add the JPanel Containter to the JFrame
    	add(pane);
    }
    
    public void buildPane()
    {   
    	pane = new JPanel();
    	// Create an Instance of the JButton Object
    	myButton = new JButton("Click Me");
    	
    	// Add the button to the JPanel
    	pane.add(myButton);
    }
    public static void main(String[] args) {
    	ButtonExample window = new ButtonExample();
    }
}



Output

 

Ad Space

Ads