Object-Oriented Software Engineering JFrames with Swing.

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Lecture 20 – Swing Layout Programming Lecturer: Prof Jim Warren.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling Events and Listeners Timers and Animation.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Object-Oriented Software Engineering Java with added Swing.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Java Programming Chapter 10 Graphical User Interfaces.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
CSE 219 Computer Science III Graphical User Interface.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Laying Out Components Interior Design for GUIs. Nov 042 What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame’s.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
1 Java Swing Layout Management. 2 Laying out components Manage realized components Manage realized components Determine size and position Determine size.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
Layout Manager Summary
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object-Oriented Software Engineering
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
3461 Laying Out Components Interior Design for GUIs.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Java Swing - Lecture 3 Layout Management
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Session 10. Java Simplified / Session 10 / 2 of 36  An Applet is a Java program that can be executed with the help of a Java enabled browser.  Every.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Graphical User Interfaces
Graphical User Interfaces -- GUIs
GUIs Model/View/Controller Layouts
University of Central Florida COP 3330 Object Oriented Programming
A Quick Java Swing Tutorial
Containers and Components
Panels & Layout Managers
GUI Programming using Java - Mouse Events
Chapter 7-3 (Book Chapter 14)
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
GUI Layouts By: Leonard & Saif.
Presentation transcript:

Object-Oriented Software Engineering JFrames with Swing

2 Contents JFrame Content Pane Layout Managers Java Interfaces for Swing ActionListeners

3 JFrame Any interesting GUI will use the JFrame class. public class FrameDemo { private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }

4 JFrame private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }

5 Content Pane The menu bar and content pane provide functionality for a JFrame.

6 JFrame Problem is createAndShowGUI is declared static: private static void createAndShowGUI() Static methods can not reference any non-static variables. This includes any reference to a non-static class. This has to be, because main has to be static, and can only reference static methods.

7 JFrame For example, move declaration of frame outside scope of createAndShowGUI: private JFrame frame; private static void createAndShowGUI() { frame = new JFrame("FrameDemo"); } $ javac FrameDemo.java FrameDemo.java:10: non-static variable frame cannot be referenced from a static context frame = new JFrame("FrameDemo");

8 Content Pane, JPanel public class MyContents extends JPanel { public MyContents ( ) { // put contents of JFrame in this // non-static constructor method } private static void createAndShowGUI() { JFrame frame = new JFrame("Person"); MyContents newContentPane = new MyContents( ); frame.setContentPane(newContentPane); }

9 Layout Managers A JPanel must have a layout manager to control where buttons, icons, text areas etc are to be placed with respect to each other inside the content pane. newContentPane LayoutManager BorderLayout CardLayout FlowLayout GridLayout GridBagLayout JPanel { choice of } * * * * *

10 BorderLayout Every content pane is initialized to use a BorderLayout. BorderLayout has five areas. These areas are specified by the BorderLayout constants PAGE_START, PAGE_END, LINE_START, LINE_END, and CENTER.

11 Code for adding buttons to BorderLayout JButton button = new JButton("Button 1 (PAGE_START)"); newContentPane.add(button, BorderLayout.PAGE_START); //Make the center component big, since that's the //typical usage of BorderLayout. button = new JButton("Button 2 (CENTER)"); button.setPreferredSize(new Dimension(200, 100)); newContentPane.add(button, BorderLayout.CENTER); button = new JButton("Button 3 (LINE_START)"); newContentPane.add(button, BorderLayout.LINE_START); button = new JButton("Long-Named Button 4 (PAGE_END)"); newContentPane.add(button, BorderLayout.PAGE_END); button = new JButton("5 (LINE_END)"); newContentPane.add(button, BorderLayout.LINE_END); Code taken from Java tutorial, also on course website.

12 BorderLayout: Note Behaviour of buttons depends on the object to which it belongs. In a later lecture we will see that buttons attached to JToolBar will seem to behave differently when the JToolBar is then attached to a BorderLayout within a JFrame!

13 BoxLayout BoxLayout stacks components either as column or in row.

14 BoxLayout Code public static void addComponentsToPane(Container pane) { pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); addAButton("Button 1", pane); addAButton("Button 2", pane); addAButton("Button 3", pane); addAButton("Long-Named Button 4", pane); addAButton("5", pane); } private static void addAButton(String text, Container container) { JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); } Code taken from Java tutorial, also on course website.

15 GridBagLayout GridBagLayout aligns components by placing them within a grid of cells Components can span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths.

16 GridBagLayout Grid (0,0)Grid (0,1)Grid (0,2) Grid (1,0) Grid (1,1) Grid (1,2) Grid (2,0)Grid (2,1)Grid (2,2)

17 GridBagLayout JButton button; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; Constraint controls where buttons appear within the GridBagLayout. Resizing component will cause horizontal space to be shared across buttons

18 GridBagLayout button = new JButton("Button 1"); c.gridx = 0; c.gridy = 0; pane.add(button, c); button = new JButton("Button 2"); c.gridx = 1; c.gridy = 0; pane.add(button, c); button = new JButton("Button 3"); c.gridx = 2; c.gridy = 0; pane.add(button, c); button = new JButton("Long-Named Button 4"); c.ipady = 40; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 1; pane.add(button, c);

19 GridBagLayout button = new JButton("5"); c.ipady = 0; //reset to default c.weighty = 1.0; //request any extra vertical space c.anchor = GridBagConstraints.PAGE_END; //bottom of space c.insets = new Insets(10,0,0,0); //top padding c.gridx = 1; //aligned with button 2 c.gridwidth = 2; //2 columns wide c.gridy = 2; //third row pane.add(button, c);

20 Java Interfaces Interface represents a contract defining a protocol. An interface is a named collection of method definitions, without implementations. An interface can also declare constants. An interface cannot implement any methods. An interface has no constructor. All interface methods are public. No static methods allowed. An interface is not part of the class hierarchy. Unrelated classes can implement the same interface. A class can implement many interfaces but can have only one superclass.

21 MouseInputListener and ActionListener > MouseInputListener void mouseClicked (MouseEvent) void mouseMoved (MouseEvent) void mouseExited (MouseEvent) void mouseReleased (MouseEvent) void mouseEntered (MouseEvent) void mousePressed (MouseEvent) void mouseDragged (MouseEvent) void actionPerformed(ActionEvent) ActionListene r

22 Event Listeners For a class to provide interaction it might implement the ActionListener and/or the MouseInputListener interfaces in Java as well as extend JPanel. MyNewClas s «interface» ActionListene r JPane l «interface» MouseInputListene r JFrame

23 Handling Events GUI Components communicate with each other by sending events. A listener is an object that is notified when an event occurs and can examine the contents of that event to perform some action. Event Listener Object GUI Component Event fired by GUI Registers to listen for particular type of events

24 Handling Events GUI Component Any number of objects can register to listen for same type of event Events are multicast to all objects that are listening Even t

25 Common Events and their Listeners > java.util.EventListene r java.awt.AWTEvent MouseEven t ActionEven t > MouseListener > MouseMotionListene r > ActionListene r

26 Listening for Events, Quick Quiz Draw a sequence diagram to show this scenario: One object acts as a source of events: aSource One object listens for events: aListener aListener registers with the source to be notified when events occur by getting aSource to execute its register( ) method. When an event occurs, aSource creates anEvent object. Then aSource causes aListener to execute its notify(anEvent) method. Then aListener queries the anEvent object by getting it to execute its query( ) method.

27 Listening For Events aSource aListener register( ) anEvent > query( ) notify(anEvent ) (2 marks for life line) (6 marks for method) (6 marks for create) (2 marks for notify) (2 marks for query) Total 20 marks