Java AWT BorderLayout Adding Label to Panel with Color

Java AWT BorderLayout Adding Label to Panel with Color:

This code is continuation of this tutorial Java AWT Adding Panels to Frame. Changes are highlighted below.

Next up handling button press events and adding Sound to button clicks.


AWT BorderLayout Adding Label to Panel with Color:

BorderLayout adding label to panel and changing color
BorderLayout adding label to panel and changing color

 

Code BorderLayout Adding Label to Panel with Color:

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;
    Label labelButtonPressHeading, labelButtonPressed;

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

        // create Label
        labelButtonPressHeading = new Label();
        labelButtonPressed = new Label();

        // 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("Help");
    }

    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 BorderLayout());
        panelLowerSection.setLayout(new GridLayout());

        // Add Label to Panel
        panelUpperSection.add(labelButtonPressHeading, BorderLayout.NORTH);
        panelUpperSection.add(labelButtonPressed, BorderLayout.CENTER);

        labelButtonPressHeading.setBackground(Color.BLACK);
        labelButtonPressHeading.setText("Buttons clicked will be shown Below:");
        labelButtonPressHeading.setForeground(Color.WHITE);

        labelButtonPressed.setBackground(Color.darkGray);

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

        // 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);
    }
}
Advertisements

One thought on “Java AWT BorderLayout Adding Label to Panel with Color

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