Showing posts with label flowlayout. Show all posts
Showing posts with label flowlayout. Show all posts

Tip for FlowLayout

FlowLayout is use to arrange components in a container from left to right. If it reached at the end of the container, but there are still left other component, it will go to the next line in the container and start again arrange component from left to right.

Example of container : JFrame, JPanel, JWindow, JInternalFrame
(Use to hold other component)

Example of component : JPanel, JButton, JTextField, JCheckBox, JRadioButton

Example source code for FlowLayout

Set JFrame layout using Flow Layout

Source code below will show you how to set JFrame layout using Flow Layout. For your information, when you use Flow Layout to set your JFrame layout, component will be add from left to right. If there are no space, next component will be add at next line from left to right.

***************************************************************************
COMPLETE SOURCE CODE FOR : FlowLayoutForJFrame.java
***************************************************************************


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;

public class FlowLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Set JFrame layout manager using Flow Layout )
JFrame frame=new JFrame("Set JFrame layout manager using Flow Layout");

//Set JFrame layout to Flow Layout
frame.setLayout(new FlowLayout());

//Create 6 buttons that want to add into JFrame
JButton button1=new JButton("BUTTON 1");
JButton button2=new JButton("BUTTON 2");
JButton button3=new JButton("BUTTON 3");
JButton button4=new JButton("BUTTON 4");
JButton button5=new JButton("BUTTON 5");
JButton button6=new JButton("BUTTON 6");

//Add all button into JFrame
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size with
//Width = 600 pixels
//Height = 400 pixels
frame.setSize(600,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

RELAXING NATURE VIDEO