Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java GUI layout

Creating a graphical user interface and placing components on it is great, but how are you going to layout those components? For this task layout managers are used.

This tutorial focuses on:

Layout manager classes

Layout manager classes are located in the java.awt package just like the classes used for displaying graphical components. So no need to to import any extra packages when you're using layout managers.

Setting a layout

You can set a frame to use a layout using the setLayout() method of the Frame class.

Example:
Frame AFrame = new Frame("Frame with components"); AFrame.setSize(450, 300); //set the layout of the frame to FlowLayout AFrame.setLayout(new FlowLayout()); AFrame.setVisible(true);

The FlowLayout class

The FlowLayout class is used to arrange components from left to right. If there is no more room, the next component will be wrapped onto a new line.

FlowLayout class constructors:

A frame using a flow layout:
import java.awt.*; class FrameWithFlowLayout{ public static void main(String[] args){ Frame AFrame = new Frame("Frame with components"); Label lblOne = new Label("This is a label"); Button btn1 = new Button("This is a button"); TextField tf1 = new TextField(); tf1.setText("This is a textbox"); AFrame.add(lblOne); AFrame.add(btn1); AFrame.add(tf1); AFrame.setSize(450, 300); //set the layout of the frame to FlowLayout //and align the components to the center AFrame.setLayout(new FlowLayout(FlowLayout.CENTER)); AFrame.setVisible(true); } }

What it will look like:

Frame with flow layout

The GridLayout class

The GridLayout class is used to arrange components in a grid of equally sized rectangular cells.

GridLayout class constructors:

A frame using a grid layout:
import java.awt.*; class FrameWithGridLayout{ public static void main(String[] args){ Frame AFrame = new Frame("Frame with components"); Label lblOne = new Label("This is a label"); Button btn1 = new Button("This is a button"); TextField tf1 = new TextField(); TextArea ta1 = new TextArea(12, 40); tf1.setText("This is a textbox"); ta1.append("Number of columns in this textarea: " + ta1.getColumns()); AFrame.add(lblOne); AFrame.add(btn1); AFrame.add(tf1); AFrame.add(ta1); AFrame.setSize(480, 300); //set the layout of the frame to GridLayout //specify the layout to have 2 rows and 2 columns AFrame.setLayout(new GridLayout(2, 2)); AFrame.setVisible(true); } }

What it will look like:

Frame with grid layout
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information