JFrame In Swing, a JFrame is similar to a window in your operating system

Similar documents
CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Chapter 12 Advanced GUIs and Graphics

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Lecture 3: Java Graphics & Events

Lecture 5: Java Graphics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Graphical User Interface

Window Interfaces Using Swing Objects

AP CS Unit 11: Graphics and Events

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Practice Midterm 1. Problem Points Score TOTAL 50

CS/120 Final Exam. Name

Graphical User Interface (GUI)

Name CS/120 Sample Exam #1 -- Riley. a) Every program has syntax, which refers to the form of the code, and, which refers to the meaning of the code.

Window Interfaces Using Swing Objects

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CSCI 201L Midterm Written Fall % of course grade

JFrame & JLabel. By Iqtidar Ali

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CS 251 Intermediate Programming GUIs: Components and Layout

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

CSCI 201L Midterm Programming. Fall % of course grade

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Dr. Hikmat A. M. AbdelJaber

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Building Java Programs Bonus Slides

Packages: Putting Classes Together

Graphical User Interfaces 2

Here is a list of a few of the components located in the AWT and Swing packages:

Please show all of your work.

Graphical User Interface (GUI)

CSCI 201L Midterm Written SOLUTION Fall % of course grade

Introduction to the JAVA UI classes Advanced HCI IAT351

PIC 20A GUI with swing

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

FirstSwingFrame.java Page 1 of 1

Starting Out with Java: From Control Structures Through Objects Sixth Edition

AP CS Unit 12: Drawing and Mouse Events

2IS45 Programming

Graphics User Defined Forms, Part I

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Java Interfaces Part 1 - Events Version 1.1

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Building Graphical User Interfaces. GUI Principles

Building Graphical User Interfaces. Overview

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Computer Science 210: Data Structures. Intro to Java Graphics

Graphical User Interfaces

Layout. Dynamic layout, Swing and general layout strategies

Chapter 13 Lab Advanced GUI Applications

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Class 16: The Swing Event Model

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Unit 6: Graphical User Interface

Top-Level Containers

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Laying Out Components. What is Widget Layout?

Unit 7: Event driven programming

Graphical User Interfaces. Comp 152

INTRODUCTION TO (GUIS)

Practice Midterm 1 Answer Key

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

TWO-DIMENSIONAL FIGURES

Java Programming Lecture 6

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Basicsof. JavaGUI and SWING

First Name: AITI 2004: Exam 2 July 19, 2004

Graphic User Interfaces. - GUI concepts - Swing - AWT

CompSci 230 S Programming Techniques. Basic GUI Components

Object Oriented Programming and Design in Java. Session 10 Instructor: Bert Huang

Containers and Components

COMP 401 Fall Recitation 6: Inheritance

Transcription:

JFrame In Swing, a JFrame is similar to a window in your operating system All components will appear inside the JFrame window Buttons, text labels, text fields, etc. 5

JFrame Your GUI program must inherit from the JFrame class JFrame has many methods and attributes that will be available to your program To use JFrame, you must import javax.swing.jframe We will need several other javax.swing classes, you can automatically include them all with import javax.swing.* //... 6

JFrame Class Diagram This class diagram shows only a small subset of the methods that you will gain when you extend JFrame JFrame «constructor» + JFrame() + JFrame(String) «update» + void add(jcomponent) + void repaint() + void setlayout(layoutmanager) + void setlocation(int,int) + void setresizable(boolean) + void setvisible(boolean) + void setsize(int,int) + void settitle(string) «query» + int getwidth() + int getheight() + int getx() + int gety() 7

Displaying Your JFrame Before showing your JFrame, you should set some of its properties, at the very least... Give the window a size Give it a screen location Give it a title And clean up some of the JFrame annoyances It is customary to set these properties in the constructor of your program public HelloSwingWorld () { // initialize JFrame here public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); 10

Absolute Positioning Screen (0,0) 12

Absolute Positioning Screen (0,0) Y X 13

Absolute Positioning Screen (0,0) Y X Height Width (Width,Height) 14

Absolute Positioning Screen (0,0) Frame (0,0) Y X Height Width (Width,Height) 15

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 16

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances width, height public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 17

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left X, Y public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 18

Title The title is the text displayed both on the title bar, and on the panel Title 19

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left settitle ("Hello Swing World"); public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 20

Setting a Layout Manager Use the setlayout(layoutmanager) method to choose the layout manager Pass null as the argument to disable layout managers BorderLayout: pass a new BorderLayout() as the argument GridLayout: pass a new GridLayout() as the argument FlowLayout: pass a new FlowLayout() as the argument 24

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances Disable layout managers public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left settitle ("Hello Swing World"); setlayout (null); public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 25

Exiting a Swing Program Normally, when you want to close a program, you click some type of x on the window In a JFrame, when you click the x, by default it only closes the frame, it does not stop the program We want to change that behavior so that when you close the frame, you also stop the program Clicking this should quit the program Style Requirement: Change the default close operation 26

Exiting a Swing Program Normally, when you want to close a program, you click some type of x on the window GOTCHA In a JFrame, when you click the x, by default it only closes the frame, it does not stop the program We want to change that behavior so that when you close the frame, you also stop the program Clicking this should quit the program Style Requirement: Change the default close operation 27

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances Disable layout managers Change default close operation public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left settitle ("Hello Swing World"); setlayout (null); setdefaultcloseoperation(jframe.exit_on_close); public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 28

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances Disable layout managers Change default close operation Disallow resizable frames public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left settitle ("Hello Swing World"); setlayout (null); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 30

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances Disable layout managers Change default close operation Disallow resizable frames public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left settitle ("Hello Swing World"); setlayout (null); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); //... 31

Displaying Your JFrame Give the window a size Give it a screen location Give it a title Clean up JFrame annoyances Disable layout managers Change default close operation Disallow resizable frames Show the frame! public HelloSwingWorld () { // initialize JFrame here setsize (400, 300); // 400 by 300 pixels setlocation (100, 100); // 100, 100 from the top left settitle ("Hello Swing World"); setlayout (null); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); public static void main (String[] args) { JFrame obj = new HelloSwingWorld(); obj.setvisible(true); 32