introduction to gui programming in java: frames, simple components, and layouts corresponds with...

46
Introduction to GUI Introduction to GUI Programming in Java: Programming in Java: Frames, Simple Frames, Simple Components, and Layouts Components, and Layouts Corresponds with Chapter Corresponds with Chapter 12 12

Upload: clyde-dorsey

Post on 17-Dec-2015

232 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Introduction to GUI Introduction to GUI Programming in Java:Programming in Java:

Frames, Simple Components, Frames, Simple Components, and Layoutsand Layouts

Corresponds with Chapter 12Corresponds with Chapter 12

Page 2: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Elements of GUI Elements of GUI ProgrammingProgramming

ComponentsComponents Visual objects that appear on the screenVisual objects that appear on the screen

LayoutsLayouts Control over the positioning of components Control over the positioning of components

within a within a containercontainer EventsEvents

Responses to user actionsResponses to user actions GraphicsGraphics

Lines, shapes, colors, fonts, etc.Lines, shapes, colors, fonts, etc.

All are encapsulated in Java Classes and Packages

Page 3: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

ComponentsComponentsTwo categories of Java Component classes:Two categories of Java Component classes:

AWT – Abstract Windows Toolkit (AWT – Abstract Windows Toolkit (java.awt java.awt packagepackage))

The older version of the componentsThe older version of the components Rely on “peer architecture”…drawing done by the OS Rely on “peer architecture”…drawing done by the OS

platform on which the application/applet is runningplatform on which the application/applet is running Considered to be “heavy-weight”Considered to be “heavy-weight”

SwingSwing ((javax.swing packagejavax.swing package)) Newer version of the componentsNewer version of the components No “peer architecture”…components draw themselvesNo “peer architecture”…components draw themselves Most are consdered to be “lightweight”Most are consdered to be “lightweight”

The textbook focuses primarily on Swing classes

Page 4: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

AWTEvent

Font

FontMetrics

Component

Graphics

Object Color

Canvas

Button

TextComponent

Label

List

CheckBoxGroup

CheckBox

Choice

Container Panel Applet

Frame

Dialog FileDialog

Window

TextField

TextArea

MenuComponent MenuItem

MenuBar

Menu

Scrollbar

LayoutManager

GUI Class Hierarchy (AWT)GUI Class Hierarchy (AWT)

Page 5: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

GUI Class Hierarchy (Swing)GUI Class Hierarchy (Swing)

Dimension

Font

FontMetrics

Component

Graphics

Object Color

Container

Panel Applet

Frame

Dialog

Window

JComponent

JApplet

JFrame

JDialog

Swing Components in the javax.swing package

Lightweight

Heavyweight

Classes in the java.awt package

1

LayoutManager

*

Page 6: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Container ClassesContainer Classes

Dimension

Font

FontMetrics

Component

Graphics

Object Color

Container

Panel Applet

Frame

Dialog

Window

JComponent

JApplet

JFrame

JDialog

Swing Components in the javax.swing package

Lightweight

Heavyweight

Classes in the java.awt package

1

LayoutManager

*

JPanel Container classes can contain other GUI components.

Page 7: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

GUI Helper ClassesGUI Helper Classes

Dimension

Font

FontMetrics

Component

Graphics

Object Color

Container

Panel Applet

Frame

Dialog

Window

JComponent

JApplet

JFrame

JDialog

Swing Components in the javax.swing package

Lightweight

Heavyweight

Classes in the java.awt package

1

LayoutManager

*

JPanel The helper classes are not subclasses of Component. They are used to describe the properties of GUI components such as graphics context, colors, fonts, and dimension.

Page 8: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Swing GUI Components Swing GUI Components

JMenuItem

JCheckBoxMenuItem

AbstractButton

JComponent

JMenu

JRadioButtonMenuItem

JToggleButton JCheckBox

JRadioButton

JComboBox

JInternalFrame

JLayeredPane

JList

JMenuBar

JOptionPane

JPopupMenu

JProgressBar

JFileChooser

JScrollBar

JScrollPane JSeparator JSplitPane

JSlider

JTabbedPane

JTable JTableHeader

JTextField JTextComponent

JTextArea

JToolBar JToolTip

JTree

JRootPane

JPanel

JPasswordField

JColorChooser

JLabel

JEditorPane

JSpinner

JButton

Page 9: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Creating GUI ObjectsCreating GUI Objects// Create a button with text OK // Create a button with text OK JButton jbtOK = new JButton("OK"); JButton jbtOK = new JButton("OK");   // Create a label with text "Enter your name: "// Create a label with text "Enter your name: "JLabel jlblName = new JLabel("Enter your name: "); JLabel jlblName = new JLabel("Enter your name: ");   

// Create a text field with text "Type Name Here"// Create a text field with text "Type Name Here"JTextField jtfName = new JTextField("Type Name Here"); JTextField jtfName = new JTextField("Type Name Here");   // Create a check box with text bold// Create a check box with text boldJCheckBox jchkBold = new JCheckBox("Bold"); JCheckBox jchkBold = new JCheckBox("Bold");   // Create a radio button with text red// Create a radio button with text redJRadioButton jrbRed = new JRadioButton("Red"); JRadioButton jrbRed = new JRadioButton("Red");   // Create a combo box with choices red, green, and blue// Create a combo box with choices red, green, and blueJComboBox jcboColor = new JComboBox(new String[]{"Red", JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"}); "Green", "Blue"});

Button

Label Text field

Check Box

Radio Button

Combo Box

Page 10: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

FramesFrames Frame is a window that is not Frame is a window that is not contained inside another contained inside another window. window.

Frame is the basis to contain Frame is the basis to contain other user interface other user interface components in Java graphical components in Java graphical applications.applications.

The Frame class can be used The Frame class can be used to create windows. to create windows.

Page 11: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Any use of Swing classes requires importing javax.swing package.

Instantiate a swing Frame object

Call JFrame methods to control visuals and behavior

Listing 12.1 p404

Page 12: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Listing 12.1 p404

Set width and height of the frame in pixels

Page 13: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Listing 12.1 p404

Cause frame to be centered on the screen when displayed

Page 14: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Listing 12.1 p404

When user closes the window, the application will terminate

Page 15: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Listing 12.1 p404

This is needed to make the frame actually show up on the screen

Page 16: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

This is what a frame looks like.

Note the title bar, the content area, the minimize, maximize/restore, and close icons.

Caption in the title bar was determined from the argument to the constructor.

Page 17: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Frames with ComponentsFrames with Components

A Frame is a container. Therefore it can A Frame is a container. Therefore it can contain other components (like buttons, contain other components (like buttons, text fields, etc.)text fields, etc.)

Components are Components are addedadded to the to the content content panepane of a frame. of a frame.

The content pane is the grey area in the The content pane is the grey area in the Frame window.Frame window.

A simplistic way to look at containment is A simplistic way to look at containment is this:this:

A JFrame contains:A JFrame contains:1.1. A menu barA menu bar2.2. A content paneA content pane

Page 18: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

A Picture of Frame A Picture of Frame ContainmentContainment

From: http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html

Actually, there’s more to it than this, but this picture will suffice for now.

Page 19: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Example: adding a component to the content pane of a Frame

Listing 12.2 p405

Page 20: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

1) Declare a reference variable for a button object.

2) Instantiate a button

3) Add the button to the content pane of the frame.

Note: prior to Java 1.5, you needed to call getContentPane() in order to obtain the frame’s content pane.

This is no longer necessary.

Page 21: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Resulting Screen

Here is the button

Page 22: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Layout ManagersLayout Managers

Control the placement of components on Control the placement of components on the container.the container.

This is an alternative to hardcoding the This is an alternative to hardcoding the pixel locations of the components.pixel locations of the components.

Advantage: resizing the container (frame) Advantage: resizing the container (frame) will not occlude or distort the view of the will not occlude or distort the view of the components.components.

Main layout managers:Main layout managers: FlowLayout, GridLayout, BorderLayout, FlowLayout, GridLayout, BorderLayout,

CardLayout, CardLayout, andand GridBagLayout GridBagLayout

Page 23: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Layout Manager HierarchyLayout Manager Hierarchy

LayoutManager is an interface. All the layout classes implement this interface

Page 24: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

FlowLayoutFlowLayoutFlowLayoutFlowLayout Places components sequentially (left-to-right) in the Places components sequentially (left-to-right) in the

order they were addedorder they were added Components will wrap around if the width of the Components will wrap around if the width of the

container is not wide enough to hold them all in a row.container is not wide enough to hold them all in a row. Default for applets and panels, but not for framesDefault for applets and panels, but not for frames Options:Options:

left, center (this is the default), or rightleft, center (this is the default), or right Typical syntax: in your Frame class’s constructor Typical syntax: in your Frame class’s constructor

setLayout(new FlowLayout(FlowLayout.LEFT)) setLayout(new FlowLayout(FlowLayout.LEFT)) OROR

setLayout(new setLayout(new FlowLayout(FlowLayout.LEFT,hgap,vgap)) FlowLayout(FlowLayout.LEFT,hgap,vgap))

Page 25: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Listing 12.3 p407: A Frame class that uses FlowLayout layout manager

Page 26: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Note: creating a subclass of JFrame

Listing 12.3 p407: A Frame class that uses FlowLayout layout manager

Page 27: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Note: it’s common to make the Frame an application class by including a main method. The main method will instantiate its own class.

Listing 12.3 p407: A Frame class that uses FlowLayout layout manager

Page 28: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

The constructor will typically do the following:1) Set the layout manager for the frame’s content pane2) Add the components to the frame’s content pane

In this case, the layout is Flow, and 6 Swing components are added

1

2

Listing 12.3 p407: A Frame class that uses FlowLayout layout manager

Swing components are in java.swing package

Layout managers are in java.awt package

Page 29: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Resizing the frame causes the components to wrap around when necessary.

Page 30: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

GridLayoutGridLayoutGridLayoutGridLayout Arranges components into rows and columnsArranges components into rows and columns In Frame’s constructor:In Frame’s constructor:

setLayoutsetLayout(new GridLayout(rows,columns))(new GridLayout(rows,columns))

OROR setLayout(new setLayout(new

GridLayout(rows,columns,hgap,vgap))GridLayout(rows,columns,hgap,vgap)) Components will be added in order, left to right, row Components will be added in order, left to right, row

by rowby row Components will be equal in sizeComponents will be equal in size As container is resized, components will resize As container is resized, components will resize

accordingly, and remain in same grid arrangementaccordingly, and remain in same grid arrangement

Page 31: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Setting the layout manager

Adding components

Listing 12.4 p409: A Frame class that uses GridLayout layout manager

Page 32: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Resizing the frame causes the components to resize and maintain their same grid pattern.

Page 33: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

BorderLayoutBorderLayoutBorderLayoutBorderLayout Arranges components into five areas: North, South, East, Arranges components into five areas: North, South, East,

West, and CenterWest, and Center In the constructor:In the constructor:

setLayout(new BorderLayout()) setLayout(new BorderLayout()) OROR

setLayout(new BorderLayout(hgap,vgap))setLayout(new BorderLayout(hgap,vgap)) for each component:for each component:

add (the_component, region)add (the_component, region) do for each area desired:do for each area desired:

BorderLayout.EAST, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.NORTH, or BorderLayout.CENTERBorderLayout.NORTH, or BorderLayout.CENTER

Behavior: when the container is resized, the components will be resized Behavior: when the container is resized, the components will be resized but remain in the same locations. but remain in the same locations.

NOTE: only a maximum of five components can be added and seen in NOTE: only a maximum of five components can be added and seen in this case, one to each region.this case, one to each region.

Page 34: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Setting the layout manager

Adding components to specific regions

Listing 12.5 pp410-411: A Frame class that uses BorderLayout layout manager

Page 35: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Resizing the frame causes the components to resize and maintain their same regions.

NOTE: the CENTER region dominates the sizing.

Page 36: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Using Panels as “Sub-Using Panels as “Sub-Containers”Containers”

JPanel is a class of special components that can JPanel is a class of special components that can contain other components.contain other components.

As containers, JPanels can have their own layout As containers, JPanels can have their own layout managers.managers.

This way, you can combine layouts within the This way, you can combine layouts within the same frame by adding panels to the frame and by same frame by adding panels to the frame and by adding other components to the panels.adding other components to the panels.

Therefore, like JFrames, you can use these Therefore, like JFrames, you can use these methods with JPanels:methods with JPanels: add()add() – to add components to the panel – to add components to the panel setLayout()setLayout() – to associate a layout manager for the – to associate a layout manager for the

panelpanel

Page 37: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Listing 12.6 p 414 Testing Listing 12.6 p 414 Testing PanelsPanels

This example uses panels to organize This example uses panels to organize components. The program creates a components. The program creates a user interface for a Microwave oven.user interface for a Microwave oven.

A button

A textfield

12

buttons

frame

p2

p1

Page 38: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Listing 12.6 p 414:

A Frame class that contains panels for organizing components

Page 39: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Creating a panel and setting its layout

Listing 12.6 p 414:

A Frame class that contains panels for organizing components

Page 40: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Adding components to the panel

Listing 12.6 p 414:

A Frame class that contains panels for organizing components

Page 41: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Creating another panel and setting its layout…note that this setting layout for the panel can be done using an overloaded constructor

Listing 12.6 p 414:

A Frame class that contains panels for organizing components

Page 42: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Adding components to the second panel…

NOTE: panel p1 is embedded inside panel p2!

Listing 12.6 p 414:

A Frame class that contains panels for organizing components

Page 43: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Adding a panel and a button to the frame’s content pane.

Note: the JFrame class’s default layout manager is Border, so you if you don’t explicitly call setLayout() for the frame it will be Border.

Listing 12.6 p 414:

A Frame class that contains panels for organizing components

Page 44: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Frame has BorderLayout manager

Button in the CENTER regionPanel p2 in the EAST region

Page 45: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Panel p2 has BorderLayout manager

Panel p1 in the CENTER region

Text field in NORTH region

Page 46: Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts Corresponds with Chapter 12

Panel p1 has GridLayout manager, four rows and three columns