Java AWT Adding Panels to Frame

Java AWT Adding Panels to Frame:

This code is continuation of this tutorial to add MenuItem to Menu. Changes that need to be made from the last code is highlighted below.

Here, GridLayout(2,1) create 2 rows and 1 column in the frame. It is very useful for creating custom designs.


AWT Adding Panels to Frame:

Creating custom design with panels
Creating custom design with panels

 

Code AWT Adding Panels to Frame:

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

    private static final int button_count = 10;

    // Declare a Frame type variable
    Frame frame;
    MenuBar menuBar;
    Menu fileMenu, optionsMenu, helpMenu;
    Panel panelUpperSection, panelLowerSection;

    // Create an array of Button type Objects
    Button [] button = new Button[button_count];

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

        // Create a panel object for organizing content
        panelUpperSection = new Panel();
        panelLowerSection = new Panel();

        // Use for loop to instantiate every button object
        for(int i = 0; i < button_count; ++i){
            button[i] = new Button( "" + i );
        }

        // Create MenuBar Object
        menuBar = new MenuBar();

        // Create Menu objects to add to the MenuBar
        fileMenu = new Menu("File");
        optionsMenu = new Menu("Options");
        helpMenu = new Menu("Exit");
    }

    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();

    }

    // 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 GridLayout(2, 1));

        // Add panel to the frame
        frame.add(panelUpperSection);
        frame.add(panelLowerSection);

        // Set Layout for the panels
        panelUpperSection.setLayout(new FlowLayout());
        panelLowerSection.setLayout(new GridLayout());

        // Add all of the buttons to the layout
        for(int i = 0; i < button_count; ++i) {
            panelLowerSection.add(button[i]);
        }

        // Register window listener event to the frame without implementing WindowListener
        frame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }
        );

        // Add Menu to the MenuBar
        menuBar.add(fileMenu);
        menuBar.add(optionsMenu);
        menuBar.add(helpMenu);

        /**
         * Add MenuItem to Menu
         */

        // Add items to File Menu
        fileMenu.add(new MenuItem("New"));
        fileMenu.add(new MenuItem("Open"));
        fileMenu.add(new MenuItem("Exit"));

        // Add items to File Menu
        optionsMenu.add(new MenuItem("Options"));

        // Add items to File Menu
        helpMenu.add(new MenuItem("Help"));
        helpMenu.add(new MenuItem("About"));

        // Add MenuBar to the Frame
        frame.setMenuBar(menuBar);

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

Java AWT Frame Adding MenuItem to Menu

Java AWT Frame Adding MenuItem to Menu:

This code is continuation of this tutorial to add Menu and MenuBar to AWT Frame. The changes from previous code is highlighted below. Create Menu objects and add MenuItem objects to them.


AWT Frame Adding MenuItem to Menu:

awt gui menu with menu items
awt gui menu with menu items

 

Code AWT Frame Adding MenuItem to Menu:

import java.awt.*;
import java.awt.event.*;

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

    private static final int button_count = 10;

    // Declare a Frame type variable
    Frame frame;
    MenuBar menuBar;
    Menu fileMenu, optionsMenu, helpMenu;

    // Create an array of Button type Objects
    Button [] button = new Button[button_count];

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

        // Use for loop to instantiate every button object
        for(int i = 0; i < button_count; ++i){
            button[i] = new Button( "" + i );
        }

        // Create MenuBar Object
        menuBar = new MenuBar();

        // Create Menu objects to add to the MenuBar
        fileMenu = new Menu("File");
        optionsMenu = new Menu("Options");
        helpMenu = new Menu("Exit");
    }

    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();

    }

    // 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 GridLayout());

        // Add all of the buttons to the layout
        for(int i = 0; i < button_count; ++i) {
            frame.add(button[i]);
        }

        // Register window listener event to the frame without implementing WindowListener
        frame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }
        );

        // Add Menu to the MenuBar
        menuBar.add(fileMenu);
        menuBar.add(optionsMenu);
        menuBar.add(helpMenu);

        /**
         * Add MenuItem to Menu
         */

        // Add items to File Menu
        fileMenu.add(new MenuItem("New"));
        fileMenu.add(new MenuItem("Open"));
        fileMenu.add(new MenuItem("Exit"));

        // Add items to File Menu
        optionsMenu.add(new MenuItem("Options"));

        // Add items to File Menu
        helpMenu.add(new MenuItem("Help"));
        helpMenu.add(new MenuItem("About"));

        // Add MenuBar to the Frame
        frame.setMenuBar(menuBar);

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

Java AWT Adding MenuBar, Menu to GUI Window

Java AWT Adding MenuBar, Menu to GUI Window:

This code is continuation of this tutorial Java AWT Creating Piano Buttons with Grid Layout.

First of all none of the buttons will work because i haven’t added any event listeners and handlers. Also menu items are not added yet.


GridLayout Adding MenuBar, Menu:

awt adding menubar menu to window
awt adding menubar menu to window

 

Code AWT GridLayout Adding MenuBar, Menu:

import java.awt.*;
import java.awt.event.*;

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

    private static final int button_count = 10;

    // Declare a Frame type variable
    Frame frame;
    MenuBar menuBar;
    Menu fileMenu, optionsMenu, exitMenu;

    // Create an array of Button type Objects
    Button [] button = new Button[button_count];

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

        // Use for loop to instantiate every button object
        for(int i = 0; i < button_count; ++i){
            button[i] = new Button( "" + i );
        }

        // Create MenuBar Object
        menuBar = new MenuBar();

        // Create Menu objects to add to the MenuBar
        fileMenu = new Menu("File");
        optionsMenu = new Menu("Options");
        exitMenu = new Menu("Exit");
    }

    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();

    }

    // 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 GridLayout());

        // Add all of the buttons to the layout
        for(int i = 0; i < button_count; ++i) {
            frame.add(button[i]);
        }

        // Register window listener event to the frame without implementing WindowListener
        frame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }
        );

        // Add Menu to the MenuBar
        menuBar.add(fileMenu);
        menuBar.add(optionsMenu);
        menuBar.add(exitMenu);

        // Add MenuBar to the Frame
        frame.setMenuBar(menuBar);

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

Java AWT Creating Piano Buttons with Grid Layout

Java AWT Creating Piano Buttons with Grid Layout:

This code is continuation of this tutorial Java AWT Changing Button Size in Flow Layout.  Just delete line 24 in the previous code and change line 45 to GridLayout in that code.


GridLayout Piano Type Buttons:

creating piano type button with awt grid layout
creating piano type button with awt grid layout

 

Code AWT GridLayout Piano Type Buttons:

import java.awt.*;
import java.awt.event.*;

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

    private static final int button_count = 10;

    // Declare a Frame type variable
    Frame frame;

    // Create an array of Button type Objects
    Button [] button = new Button[button_count];

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

        // Use for loop to instantiate every button object
        for(int i = 0; i < button_count; ++i){
            button[i] = new Button( "" + i );
        }

    }

    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();

    }

    // 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 GridLayout());

        // Add all of the buttons to the layout
        for(int i = 0; i < button_count; ++i) {
            frame.add(button[i]);
        }

        // Register window listener event to the frame without implementing WindowListener
        frame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }
        );

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