Skip to content

A simple Java GUI using a layout manager

September 12, 2011

This example builds upon the simple example titled.  This example demonstrates how to use a layout manager class to assist when placing multiple elements into a GUI.  Java does not have the *benefit of using HTML tables or Divs to assist in the layout and grouping of elements in a GUI.

First, your GUI has 2 types of entities, containers and non- containers.  You GUI will likely use a frame to house the elements that you want to place into it.  A frame is the outer most entity, with other more targeted elements contained within the entity.

Think of the frame as the outer most entity. The frame is used to hold things.

The panel is another container that is used within frames to further group and organize GUI elements.

GUI elements, like buttons, labels, text areas, text fields, radio buttons, checkboxes and more are usually added to a panel or directly to a frame.

Layout managers are like templates that you can apply to containers like frames and panels to further specify how you would like your GUI elements to be positioned.  Below is an example of a simple GUI that features

  • an outer frame
  • a panel
  • a layout manager called FlowLayout used on the panel
  • 2 registered buttons added to the panel
  • the panel added to the frame
  • the frame prepared and show
  • 2 separate classes implementing the ActionListener interface (class)
  • Listener classes implementing interface using the JOptionPane method to show message

This example includes its own main method that is responsible for creating an object of itself.  The class has a constructor that is  building the GUI.

 

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Buttons_implemented {

    //constructor
    public Buttons_implemented(){
        JButton btn = new JButton("Button 1");
        JButton btn2 = new JButton("Button 2");

        //register button listener events
        btn.addActionListener(new Btn_ClickHandler());
        btn2.addActionListener(new Btn2ClickHandler());

        //add a container element to place buttons
        JFrame main_frame = new JFrame();
        main_frame.setLayout(new FlowLayout());

        //add buttons to main_frame
        main_frame.add(btn);
        main_frame.add(btn2);

        //prepare the frame for display
        main_frame.pack();
        main_frame.setVisible(true);
    }

    public static void main(String args[]){
        //create new object to demo
        new Buttons_implemented();
    }

}

 class Btn_ClickHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //some action
        JOptionPane.showMessageDialog(null, "button 1 clicked", "title", JOptionPane.ERROR_MESSAGE);
    }
}

 class Btn2ClickHandler implements ActionListener{
     public void actionPerformed(ActionEvent e){
         //something...
         JOptionPane.showMessageDialog(null, "button 2 clicked", "title", JOptionPane.ERROR_MESSAGE);
     }
 }
Advertisements

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: