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

Simple Gui Button Example

01/**
02 *  Application: Gui Button Example
03 *  Author: Brian SonniE
04 *  5/11/09
05 *   www.fluidcoding.com - Coding Simplified.
06 */
07  
08import javax.swing.*;
09  
10public class ButtonExample extends JFrame{
11    private JButton myButton;
12    private JPanel pane;
13     
14    public ButtonExample()
15    {
16        // Make application close when exit is clicked
17        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18        // Set the title on the top of the window
19        setTitle("Button Example");
20        // make it visible
21        setVisible(true);
22        // set the size (Width, Height) of window
23        setSize(400,100);
24        // Build the JPanel
25        buildPane();
26        // Add the JPanel Containter to the JFrame
27        add(pane);
28    }
29     
30    public void buildPane()
31    {  
32        pane = new JPanel();
33        // Create an Instance of the JButton Object
34        myButton = new JButton("Click Me");
35         
36        // Add the button to the JPanel
37        pane.add(myButton);
38    }
39    public static void main(String[] args) {
40        ButtonExample window = new ButtonExample();
41    }
42}

Output

 

Ad Space

Ads