Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

The key points are:Layout, Managers, Reminder, Creation, Flow Layout, Grid Layout, Border Layout, Box Layout, Combining Layouts, Improving the Appearance

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives
describe the basic layout managers for
GUIs
14. GUI Layout
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

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