Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Introduction to Java 2 Programming
Java Software Development Paradigm Lecture # 12. Basics of GUI.
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
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
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)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
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.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
GUI in Java. Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
Java Applet. Introductions Applet is java program that can be embedded into HTML pages Java applets runs on the java enables web browsers such as mozila.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
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.
1 Chapter 16: Creating Windows. 2 Basics of GUI Programming How to create a Window. TryWindow.java You always need a JFrame component before creating.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Swing JComponents.
Java Swing.
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
GUIS: Graphical User Interfaces
IFS410: Advanced Analysis and Design
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Creating a Window

A basic window in Java is represented by an object of the class Window in the package java.awt.

JFrame The library class JFrame, defined in javax.swing is a more useful class for creating a window since it provides more facilities. Its superclasses are: Object->Component->Container->Window-> Frame->JFrame

JFrame object: contains 200+ methods as it has 5 superclasses from which it inherits members. Displays an application by creating an object of JFrame, calling a method for the object to set the size of the window, then calling a method to display the window. See TestWindow.java OR ShowFame.java

DISPOSE_ON_CLOSE: frame and any components in it are destroyed. DO_NOTHING_ON_CLOSE: makes the close operation for the frame window ineffective. HIDE_ON_CLOSE: hides window by setting visible to false. This is the default action if you don’t call the setDefaultCloseOperation() method.

Key Classes JFrame –used as the basic Java application window –an object of this class has a title bar and provision for adding a menu –you add other components to it –use subclasses to create a window class specific to your application –add GUI components or draw in this window if required

Key Classes (contd) JDialog –used to define a dialog window that is used for entering data into a program in various ways

Key Classes (contd) JApplet –a program designed to run embedded in a web page. All applets have this class as a base –you can draw in a JApplet object and also add menus and other components

Key Classes (contd) JComponent –subclasses of JComponent define menus, buttons, checkboxes, etc. –these classes are used to create the GUI for your application

Window Panes when you want to add GUI components or draw in a window displayed from a JFrame object, the components are added to a window pane. The same for an applet. window panes are objects that are containers which represent an area of a window. Most of the time a window pane called the content pane is used.

The JFrame class defines methods to provide a reference to any of the panes. getContentPane() returns the content pane as type Container. This is the method used most frequently, since you normally add components to the content pane.

Swing Components JButton class –defines a regular pushbutton –this component has a border of type BevelBorder added to it. JCheckBox class –clicking on the checkbox changes it state from checked to unchecked

JRadioButton class –operate in a group where only one button can be in a pressed state at any one time. JMenuBar class –defines a member usually found at the top of an application window JMenu class –object represents a top-level menu item on a menubar JCheckBoxMenuItem class –component is a menu item with a check that is ticked when the item is selected.

JLabel class –passive and does not react to input events so you can’t edit it. JTextField –displays a single line of text, but is editable. JTextArea –allows editing of multi-line text

Adding Components to a Container –The components stored in a container are recorded in an array within the container object. The array is increased in size when necessary to accommodate as many components as are present. –To add a component to a Container use the add( ) method.

The Size and Position of a Component Position is defined by x and y coordinates of type int Size is defined by width and height. –Components have a preferred size which depends on the particular object eg. The preferred size of a JButton object that defines a button is the size that accommodates the label for the button.

Methods to retrieve or alter size and position void setBounds(int x, int y, int width, int height - sets the position of the Component object to the coordinates(x,y), and the width and height of the object to the values defined by the 3rd and 4th arguments.

Toolkit class –contains information about the environment in which your application is running, including the screen size in pixels. Use the getToolkit() method to help set the size and position of a window on the screen.

Visual Characteristics of a Component void setBackground(Color bColor) - sets background color to bColor. Color getBackground( ) - retrieves the current background color

Container Layout Manager responsible for arranging components in a certain way in the container. Layout manager for a container determines the position and size of all the components in the container.

–FlowLayout - layout manager which places components in successive rows in a container, fitting as many on each row as possible, and starting on the next row as soon as a row is full. It is the default manager for JPanel objects. –BorderLayout - places components against the 4 borders of the container and in the center. Default manager for JFrame, JDialog, JApplet objects.

–GridLayout - places components in the container in a rectangular grid with the number of rows and columns you specify. –GridBagLayout - the most flexible and powerful of the layout managers, also more complicated to use. Components in the GBLayout need not be the same size as in Glayout, and the order in which the components are placed is arbitrary.

Setting the layout manager To set the layout manager of a container you call the setLayout( ) method for the container. FlowLayout flow = new FlowLayout( ); aWindow.getContentPane( ).setLayout(flow); Note: we cannot add components directly to a JFrame object, we must add them to the content pane for the window. FlowLayout flow = new FlowLayout(FlowLayout.LEFT,20,30); //sets hgap & vgap

See TryFlowLayout TryAppletLayout