TCU CoSc 10403 Programming with Java The JFrame Class.

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
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.
Swing Components III Chapter 12 - Student JFrame, Component Methods.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
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.
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.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
JAPPLET.
PROG Object Oriented Programming II PROG Object Oriented Programming II Window Events Multi-screen Applications.
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.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
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.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Java Graphics Graphical Components as objects. Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
GUIs and Events Rick Mercer.
Graphical User Interfaces -- GUIs
Java Swing.
A Quick Java Swing Tutorial
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
A Quick Java Swing Tutorial
Advanced Programming in Java
GUI and Event-Handling Programming
Presentation transcript:

TCU CoSc Programming with Java The JFrame Class

A Frame is a top-level window with a title and a border. The size of the frame includes any area designated for the border. Calling setVisible(true) makes the frame appear onscreen. Sometimes you might see the show() method used instead. The two usages are equivalent. By default, the frame window will appear in the top left corner of the screen. Another location can be specified using the setlocation() method on the frame. The default layout manager is BorderLayout. Five steps to get a frame to work: 1. Create the frame: JFrame frame = new Jframe(“** some title **”); 2. Set the layout : frame.setLayout(new FlowLayout()); 3. Instantiate the components and add them to the frame. frame.add(button1); 4. Size the frame: frame.setSize(200,500); 5. Set the location: frame.setLocation(100,200); 6. Show the frame: frame.setVisible(true);

The JFrame Class Two Constructors that are of interest to us – JFrame() Constructs a new frame that is initially invisible. JFrame(String title) Creates a new, initially invisible Frame with the specified title. Sizing a JFrame : JFrame frame1 = new JFrame(“CoSc 10403”);... frame1.setSize(100,100); //will appear in upper left area of screen frame1.setSize(100,100); //frame of size 100x100 pixels frame1.setLocation(100,50); //placement 100 pixels over, 50 pixels down

The JFrame Class Useful methods that are of interest to us – void pack() Size the window on the basis of the minimum size to fit all components added to the frame (i.e., just wide enough to fit the text in the bottom of the frame and high enough for each component to display properly). void setSize(int, int) Set the total size of the window void setBounds(int, int, int, int) Set the size and position of the window. void setLocation(int, int) Set the location of the upper left corner of the window void remove(Component comp) Removes the specified component from this container. void removeAll() Removes all components from the JFrame. void setLayout(LayoutManager manager) Sets the layout of this component.

The JFrame Class import javax.swing.*; import java.awt.event.*; import java.awt.*; public class JFrameDemo extends JApplet implements ActionListener { JLabel l1 = new JLabel("Welcome to my JFrame"); JButton b1 = new JButton("Display Frame"); JFrame frame; public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent e) { displayJFrame("Put this text on the JFrame"); } public void displayJFrame(String s) { frame = new JFrame("My JFrame Demo"); frame.setBounds(300,0,200,100); frame.setBackground(Color.BLUE); frame.setLayout(new FlowLayout()); frame.add(l1); l1.setForeground(Color.WHITE); l1.setText(s); frame.setVisible(true); }

How do you close a JFrame? You can: –Get rid of the JFrame object entirely. –Just make the JFrame invisible, but ready to be made visible again an instant later. –Make the JFrame invisible, and put in into cold storage, ready to be made visible again with a little thawing. –System.exit( 0 ) the program immediately You can close the JFrame: –when the user clicks the closing X. –when the user clicks a close menu item. –programmatically when you decide to close the JFrame. –programmatically when you decide to shut down the entire program When the user clicks the close X, that does not make your JFrame object mysteriously disappear or force it to close, or go invisible. All it does is send your JFrame a WindowClosing event. Your JFrame is free to ignore the event, close the JFrame in any of the four usual ways, or do something else entirely.The event in no way closes your JFrame or changes its state. It is totally up to you what happens. Receiving a WindowClosingEvent is not an eviction notice; it is just notification the user idly clicked an X. It does not mean the JFrame is closing or that it has to close, just that the user would like it to. JFrame Details

Shorthand WindowClosingEvent Handlers There are three shortcuts to writing WindowClosing event handlers. With these shortcuts, you have no opportunity to add any of your own application code. If all you want to do is call dispose() you can use this, instead of setting up an event handler. this.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE ); If all you want to do is call setVisible( false ) you can use this, instead of setting up an event handler. this.setDefaultCloseOperation ( JFrame.HIDE_ON_CLOSE ); Believe it or not, this is the default. Beware! If all you want to do is call System.exit( 0 ) you can use this, instead of setting up an event handler. this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); If all you want to write your own custom event handler, you need to use this, as well as setting up an event handler. this.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); JFrame Details

this.setDefaultCloseOperation ( JFrame. DO_NOTHING_ON_CLOSE ); // what happens when user closes the JFrame. WindowListener windowListener = new WindowAdapter() { // anonymous WindowAdapter class public void windowClosing ( WindowEvent w ) { // Whatever application code you want to do on close, e.g. rememberLocation( MyFrame.this.getX(),MyFrame.this.getY() ); // Whatever code you want to actually close the JFrame, e.g. MyFrame.this.setVisible( false ); MyFrame.this.dispose(); } // end windowClosing };// end anonymous class this.addWindowListener( windowListener ); JFrame Details