Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.

Slides:



Advertisements
Similar presentations
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Advertisements

Drawing in a frame – Java GUI
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Object-Oriented Software Engineering Java with added Swing.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Graphical User Interface (GUI) Design using Swing Course Lecture Slides.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
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.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
GUI programming Graphical user interface-based programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Java exercise review Lesson 25: Exercise 1, 2 and 3.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
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.
The Drawing program – Java Applets
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Even-Driven Programming and basic Graphical User Interface.
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.
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
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.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
Applets Yong Choi School of Business CSU, Bakersfield.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
1 Applets. 2 Design of Applets 3 Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Basics of GUI Programming Chapter 11 and Chapter 22.
Java Swing One of the most important features of Java is its ability to draw graphics.
First Programs Chapter 2 The Java language (compiler) on CD ROM Create a program using an editor Compile the program Run the program Integrated Development.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Lesson 33: Layout management and drawing – Java 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.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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:
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A structured walkthrough
Multiple buttons and action calls
A First Look at GUI Applications
Chapter 4 Interface Types and Polymorphism Part 2
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
UNIT-5.
Steps to Creating a GUI Interface
The calculator – Java GUI
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

Lesson 27: Introduction to the Java GUI

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button Invoking the graphics libraries called the abstract window toolkit and the swing. We will use the classes provided in these standard java libraries to create our Java GUI. While we can code this ourselves, this saves us a lot of work.

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button Our own class. The file it is stored in must therefore be called HelloButton.java. Our main section. This means that this will be an executable file that we can call using the command “java HelloButton”. As all main sections must do, it accepts arguments

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button Here we create a java frame (JFrame), also known as a window. The JFrame is available since we imported the Swing library. This is a constructor statement. We identify it through the keyword “new” This is the title of the frame

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button We use the method getContentPane() to get a reference to JFrame’s special container. A container is a component to which we can add other components. A JFrame is actually a container, but we should not add components directly to a JFrame. Instead, we should add all components to a special container in the JFrame.

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button Here we create a Javax.swing.JButton object and give it a string which is used as a label. The object is called hello

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button Note: For now, we do not tell the container how to arrange this component, nor where this component is to be displayed (we will do this later). The method add in the class Container is used to add a component to the JFrame’s content pane (later we will add many components to a frame).

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button The pack method from JFrame is used to minimize the window to smallest size that can accommodate the content of the container Since we have only one component in the container, the window will be minimized to the size of the button, which is determined by the size of the label.

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button The show method from JFrame is used to display the frame.

The result The source fileCompiling and running the program The output

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button The Pack method caused the frame to cut off the label of the frame.

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } The Java Button The Pack method caused the frame to cut off the label of the frame.

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.setSize(300,200); frame.show(); } The Java Button We can set the size of the frame by using the method setSize from JFrame. This method accepts 2 parameters (width, height), which passed in terms of number of pixels.

The new result The source fileCompiling and running the program The output

Exercise # 5 Complete exercise 5 of the Java code handed out in-class. Upload your *.java file (not the class file) on BlackBoard by Friday March 21 st 2003 (10 am). 1.Write the psudocode and the deployment diagram for what you are planning to do 2.Separate the Ball class into 2 classes: Ball.java that known the location, its size and how to paint itself MovableBall.java that extends the class Ball and adds all behaviors related to motion. Such as the data fields dx and dy, the functions setMotions, move and so on. 3.Rewrite the MultiBallWorld to use an instance of MovableBall. IMPORTANT: Don’t built on what we did in exercise #4 or earlier exercises. Instead use the original files for Ball and MultiBallWorld posted on BlackBoard.