Skip to content

Instantly share code, notes, and snippets.

@figengungor
Created March 9, 2013 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save figengungor/5126354 to your computer and use it in GitHub Desktop.
Save figengungor/5126354 to your computer and use it in GitHub Desktop.
Java GUI >> FlowLayout
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyGUI extends JFrame
{
JPanel jp; //Made global so all methods can use this
//GUI representation of System.out.println();
JButton jb1,jb2,jb3;
// panels are containers for objects like buttons, labels etc...
public MyGUI()
{
setTitle("Tutorial");
setSize(600,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp = new JPanel();
//jp.setLayout(new FlowLayout()); //buttons aligned in the middle
//jp.setLayout(new FlowLayout(2)); //buttons aligned 2 pixel far from right border of the frame
//jp.setLayout(new FlowLayout(-2)); //buttons aligned 2 pixel far from left border of the frame
jp.setLayout(new FlowLayout(1, 100, 100)); // align from left to right, horizantal gap, vertical gap
jb1 = new JButton("Button1");
jb2 = new JButton("Button2");
jb3 = new JButton("Button3");
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
add(jp);
/*Constructor Purpose
FlowLayout()
Constructs a new FlowLayout object with a centered alignment and
horizontal and vertical gaps with the default size of 5 pixels.
FlowLayout(int align)
Creates a new flow layout manager with the indicated alignment
and horizontal and vertical gaps with the default size of 5 pixels.
The alignment argument can be FlowLayout.LEADING,
FlowLayout.CENTER, or FlowLayout.TRAILING.
When the FlowLayout object controls a container with a left-to right component
orientation (the default), the LEADING value specifies the components
to be left-aligned and the TRAILING value specifies the components to be right-aligned.
FlowLayout (int align, int hgap, int vgap)
Creates a new flow layout manager with the indicated alignment and
the indicated horizontal and vertical gaps.
The hgap and vgap arguments specify the number of pixels to put between components.*/
}
public static void main(String[] args)
{
MyGUI frame = new MyGUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment