Docsity

Layout - Introduction to Programming in Java - Lecture Slides, Slides of Network security

Partial preview of the text

Download Layout - Introduction to Programming in Java - Lecture Slides and more Slides Network security in PDF only on Docsity!

Objectives

  • describe the basic layout managers for

GUIs

  1. GUI Layout

Contents

  1. A Reminder on GUI Creation
  2. Flow Layout
  3. Grid Layout
  4. Border Layout
  5. Box Layout
  6. Combining Layouts
  7. Improving the Appearance
  8. Other Layout Managers

1.1. Emphasis of this Part

• The examples in this part will concentrate on

layout managers and the JPanel container

– step 3

• Layout managers examined:

– FlowLayout, GridLayout, BorderLayout,

BoxLayout, combining layouts

Basic Layouts as Pictures

flow

grid

N

C

S

W E

border

box

FlowDemo.java

import java.awt.; import java.awt.event.; import javax.swing.*; public class FlowDemo extends JFrame { public FlowDemo() { super("E-Commerce Application"); Container c = getContentPane(); c.setLayout( new FlowLayout() ); :

JCheckBox jck1 = new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); c.add( jck1 ); c.add( jck2 ); c.add( jck3 ); JButton jb1 = new JButton("place order"); c.add( jb1 ); JButton jb2 = new JButton("cancel"); c.add( jb2 ); JLabel jl = new JLabel(new ImageIcon("bmw.jpg")); c.add(jl); :

Initial Appearance

After Resizing

There is now space for everything on one line.

3. Grid Layout

• GridLayout places components in a grid,

specified in terms of the number of rows

and columns

– the spacing between the grid cells can also be

specified

• Some of the components are resized to fit

the grid cell they appear inside

– doesn't look nice

continued

2x

  • GridDemo.java contains one major change

from FlowDemo.java:

c.setLayout( new GridLayout(3,2,10,7);

  • 3 rows, 2 columns, 10 pixel horizontal spacing,

7 pixel vertical spacing

  • The other change is to increase the vertical size of

the frame:

setSize(400, 400 );

JCheckBox jck1 = new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); c.add( jck1 ); c.add( jck2 ); c.add( jck3 ); JButton jb1 = new JButton("place order"); c.add( jb1 ); JButton jb2 = new JButton("cancel"); c.add( jb2 ); JLabel jl = new JLabel(new ImageIcon( bmw.jpg")); c.add(jl); :

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,400); setLocationRelativeTo(null); // center the window setVisible(true); } // end of GridDemo() public static void main(String[] args) { new GridDemo(); } } // end of GridDemo class

GridDemoP.java

• Components can be protected from resizing

by being placed inside a JPanel

– the panel is resized instead

// use a panel to stop the cancel button growing JPanel p = new JPanel(); JButton jb2 = new JButton("cancel"); p.add(jb2); c.add( p ); :

Appearance

the 'cancel'

button has

not been

resized