Layout Manager Layout Managers
Automatically control placement of components in a panel Layout Manager Heuristics
null FlowLayout GridLayout
none, Left to right, programmer Top to bottom sets x,y,w,h
BorderLayout CardLayout GridBagLayout n w e c One at a time JButton
s Placing components in a JFrame
Layout manager: Instance controls placement of components.
JFrame layout manager default: BorderLayout.
BorderLayout layout manager: Can place 5 components: Placing components in a JFrame
public class C extends JFrame { public C() { Container cp= getContentPane(); JButton jb= new JButton( Click here ); JLabel jl= new JLabel( label 2 ); cp.add(jb, BorderLayout.EAST); North cp.add(jl, BorderLayout.WEST); pack(); West Center East setVisible(true); South } JFrameDemo.java Placing components in a JFrame import java.awt.*; import javax.swing.*;
/** Demonstrate placement of components in a JFrame. Places five components in 5 possible areas: (1) a JButton in the east, (2) a JLabel in the west, (3) a JLabel in the south, (4) a JTextField in the north (5) a JTextArea in the center. */ Placing components in a JFrame public class ComponentExample extends JFrame { /** Constructor: a window with title t and 5 components */ public ComponentExample(String t) { super(t); Add components to Container cp= getContentPane(); its contentPane cp.add(new JButton("click me"), BorderLayout.EAST); cp.add(new JTextField("type here", 22), BorderLayout.NORTH); cp.add(new JCheckBox("I got up today"), BorderLayout.SOUTH); cp.add(new JLabel("label 2"), BorderLayout.WEST); cp.add(new JTextArea("type\nhere", 4, 10), BorderLayout.CENTER); pack(); } ComponentExample.java Placing components in a JPanel
JPanel layout manager default: FlowLayout.
FlowLayout layout manager: Place any number of components. They appear in the order added, taking as many rows as necessary. Placing components in a JPanel
import java.awt.*; import javax.swing.*; /** Instance has labels in east /west, JPanel with four buttons in center. */ public class PanelDemo extends JFrame { JPanel p= new JPanel(); /** Constructor: a frame with title "Panel demo", labels in east/west, blank label in south, JPanel of 4 buttons in the center */ public PanelDemo() { JPanel as a super("Panel demo"); p.add(new JButton("0")); p.add(new JButton("1")); container p.add(new JButton("2")); p.add(new JButton("3")); Container cp= getContentPane(); cp.add(new JLabel("east"), BorderLayout.EAST); cp.add(new JLabel("west"), BorderLayout.WEST); cp.add(new JLabel(" "), BorderLayout.SOUTH); cp.add(p, BorderLayout.CENTER); pack(); } } Combinations
JButton JButton
JTextArea Combinations JButton JButton JFrame
n JPanel: FlowLayout JPanel: BorderLayout
c
JTextArea Code: null layout
JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null); // x,y layout p.add(b); f.setContentPane(p); press me Code: FlowLayout
JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton(“press me”); JButton b2 = new JButton(“then me”); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); press me then me
Set layout mgr before adding components LAB #2
Create a UI for ordering pizza. These slides are from:
• infovis.cs.vt.edu/oldsite/GUI/java/Java2-Swing.ppt, made by Chris North for CS3724
• Some of these slides are made by Seonah Lee