Creating a Java AWT window

Creating a Java AWT window:

The code below creates a window with a given title and given size. Since i haven’t handled window closing event it will not close on clicking the X button. So to close the window stop the execution from the IDE or end that process.

Sample Window:

awt gui creation example
awt gui creation example

 

AWT GUI Window Code:

import java.awt.*;

/**
 * Created by asif on 8/13/2015.
 */
public class SimpleAwtGui {

    // Declare a Frame type variable
    Frame frame;

    public SimpleAwtGui(){
         // Create Frame Object and pass in the Frame Name / title
        frame = new Frame("AWT GUI EXAMPLE");
    }

    /*
     * Not necessary but good practice all codes inside this can be written inside main
     */
    public void showFrame(){
        // set the size of the window
        frame.setSize(400, 400);

        // set the layout for the window
        frame.setLayout(new FlowLayout());

        // set the frame visible otherwise nothing will be shown
        frame.setVisible(true);
    }

    public static void main(String [] args){

        // Create an instance of SimpleAwtGui
        SimpleAwtGui window = new SimpleAwtGui();

        // call the showFrame() function to display the window
        window.showFrame();

    }
}
Advertisements

One thought on “Creating a Java AWT window

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s