Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 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.
Corresponds with Chapter 12
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 13 GUI Basics.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 GUI Basics First three slides are repeated from previous lecture notes! You will probably (in fact should) never write Java codes like the program examples.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
GUI and event-driven programming An introduction.
Advanced Java and Android Programming Day 1: Introduction to GUI Programming Intro to GUI Programming1.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
GUI Basics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Graphical User Interfaces Tonga Institute of Higher Education.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit 6 Graphical user interfaces 1.
Chapter 7 A First Look at GUI Applications Layout Managers.
Modern Programming Language Java
Java Swing.
Timer class and inner classes
Creating Graphical User Interfaces
Graphical User Interface
Chapter 7-3 (Book Chapter 14)
GUI building with the AWT
Chapter 12 GUI Basics.
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts

Elements of GUI Programming Components Components Visual objects that appear on the screen Visual objects that appear on the screen Layouts Layouts Control over the positioning of components within a container Control over the positioning of components within a container Events Events Responses to user actions Responses to user actions Graphics Graphics Lines, shapes, colors, fonts, etc. Lines, shapes, colors, fonts, etc. All are encapsulated in Java Classes and Packages

Components Two categories of Java Component classes: AWT – Abstract Windows Toolkit (java.awt package) AWT – Abstract Windows Toolkit (java.awt package) The older version of the components The older version of the components Rely on “peer architecture”…drawing done by the OS platform on which the application/applet is running Rely on “peer architecture”…drawing done by the OS platform on which the application/applet is running Considered to be “heavy-weight” Considered to be “heavy-weight” Swing(javax.swing package) Swing(javax.swing package) Newer version of the components Newer version of the components No “peer architecture”…components draw themselves No “peer architecture”…components draw themselves Most are consdered to be “lightweight” Most are consdered to be “lightweight”

Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblName = new JLabel("Enter your name: "); // Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here"); // Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blue JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"}); "Green", "Blue"}); Button LabelText field Check Box Radio Button Combo Box

Frames Frame is a window that is not contained inside another window. Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java graphical applications. Frame is the basis to contain other user interface components in Java graphical applications. The Frame class can be used to create windows. The Frame class can be used to create windows.

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

Set width and height of the frame in pixels

Listing 12.1 p404 Cause frame to be centered on the screen when displayed

Listing 12.1 p404 When user closes the window, the application will terminate

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

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.

Frames with Components A Frame is a container. Therefore it can contain other components (like buttons, text fields, etc.) A Frame is a container. Therefore it can contain other components (like buttons, text fields, etc.) Components are added to the content pane of a frame. Components are added to the content pane of a frame. The content pane is the grey area in the Frame window. The content pane is the grey area in the Frame window. A simplistic way to look at containment is this: A simplistic way to look at containment is this: A JFrame contains: A JFrame contains: 1. A menu bar 2. A content pane

A Picture of Frame Containment From: Actually, there’s more to it than this, but this picture will suffice for now.

Example: adding a component to the content pane of a Frame Listing 12.2 p405

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.

Resulting Screen Here is the button

Layout Managers Control the placement of components on the container. Control the placement of components on the container. This is an alternative to hardcoding the pixel locations of the components. This is an alternative to hardcoding the pixel locations of the components. Advantage: resizing the container (frame) will not occlude or distort the view of the components. Advantage: resizing the container (frame) will not occlude or distort the view of the components. Main layout managers: Main layout managers: FlowLayout, GridLayout, BorderLayout, CardLayout, and GridBagLayout FlowLayout, GridLayout, BorderLayout, CardLayout, and GridBagLayout

Layout Manager Hierarchy LayoutManager is an interface. All the layout classes implement this interface

FlowLayoutFlowLayout Places components sequentially (left-to-right) in the order they were added Places components sequentially (left-to-right) in the order they were added Components will wrap around if the width of the container is not wide enough to hold them all in a row. Components will wrap around if the width of the container is not wide enough to hold them all in a row. Default for applets and panels, but not for frames Default for applets and panels, but not for frames Options: Options: left, center (this is the default), or right left, 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)) OR setLayout(new FlowLayout(FlowLayout.LEFT,hgap,vgap))

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

Note: creating a subclass of JFrame Listing 12.3 p407: A Frame class that uses FlowLayout layout manager

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

The constructor will typically do the following: 1)Set the layout manager for the frame’s content pane 2)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

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

GridLayoutGridLayout Arranges components into rows and columns Arranges components into rows and columns In Frame’s constructor: In Frame’s constructor: setLayout setLayout (new GridLayout(rows,columns)) OR setLayout(new GridLayout(rows,columns,hgap,vgap)) setLayout(new GridLayout(rows,columns,hgap,vgap)) Components will be added in order, left to right, row by row Components will be added in order, left to right, row by row Components will be equal in size Components will be equal in size As container is resized, components will resize accordingly, and remain in same grid arrangement As container is resized, components will resize accordingly, and remain in same grid arrangement

Setting the layout manager Adding components Listing 12.4 p409: A Frame class that uses GridLayout layout manager

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

BorderLayoutBorderLayout Arranges components into five areas: North, South, East, West, and Center Arranges components into five areas: North, South, East, West, and Center In the constructor: In the constructor: setLayout(new BorderLayout()) setLayout(new BorderLayout()) OR OR 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.NORTH, or BorderLayout.CENTER BorderLayout.EAST, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.NORTH, or BorderLayout.CENTER Behavior: when the container is resized, the components will be resized but remain in the same locations. Behavior: when the container is resized, the components will be resized but remain in the same locations. NOTE: only a maximum of five components can be added and seen in this case, one to each region. NOTE: only a maximum of five components can be added and seen in this case, one to each region.

Setting the layout manager Adding components to specific regions Listing 12.5 pp : A Frame class that uses BorderLayout layout manager

Resizing the frame causes the components to resize and maintain their same regions. NOTE: the CENTER region dominates the sizing.

Using Panels as “Sub-Containers” JPanel is a class of special components that can contain other components. JPanel is a class of special components that can contain other components. As containers, JPanels can have their own layout managers. As containers, JPanels can have their own layout managers. This way, you can combine layouts within the same frame by adding panels to the frame and by adding other components to the panels. This way, you can combine layouts within the same frame by adding panels to the frame and by adding other components to the panels. Therefore, like JFrames, you can use these methods with JPanels: Therefore, like JFrames, you can use these methods with JPanels: add() – to add components to the panel add() – to add components to the panel setLayout() – to associate a layout manager for the panel setLayout() – to associate a layout manager for the panel

Listing 12.6 p 414 Testing Panels This example uses panels to organize components. The program creates a user interface for a Microwave oven.

Listing 12.6 p 414: A Frame class that contains panels for organizing components

Creating a panel and setting its layout Listing 12.6 p 414: A Frame class that contains panels for organizing components

Adding components to the panel Listing 12.6 p 414: A Frame class that contains panels for organizing components

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

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

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

Frame has BorderLayout manager Button in the CENTER region Panel p2 in the EAST region

Panel p2 has BorderLayout manager Panel p1 in the CENTER region Text field in NORTH region

Panel p1 has GridLayout manager, four rows and three columns