Top Banner
https://www.facebook.com/Oxus20 [email protected] JAVA GUI PART I Milad Kawesh » GUI ˃ Introduction ˃ AWT Package ˃ Swing Package ˃ Frame vs. JFrame
17

JAVA GUI PART I

May 06, 2015

Download

Education

OXUS 20

A Graphical User Interface (GUI) is a user interface based on graphics i.e. icons, pictures, menus, etc. instead of just plain text, it uses a mouse as well as a keyboard as an input device.

GUI applications enable the users (especially naive ones) to interact with a system easily and friendly. This presentation is meant for the individual who has little or no experience in Java GUI programming.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JAVA GUI PART I

https://www.facebook.com/Oxus20

[email protected]

JAVA GUI

PART I Milad Kawesh

» GUI ˃ Introduction

˃ AWT Package

˃ Swing Package

˃ Frame vs. JFrame

Page 2: JAVA GUI PART I

Agenda

» Introduction

˃ GUI (Graphical User Interface)

˃ Simple Example

» AWT and Swing Package

˃ AWT

˃ Swing

˃ JFrame vs. Frame

˃ Simple Example

2

https://www.facebook.com/Oxus20

Page 3: JAVA GUI PART I

Introduction

» A Graphical User Interface (GUI) presents a

user-friendly mechanism for interacting with

an application.

˃ Gives an application a distinctive "look" and "feel".

˃ Consistent, intuitive user-interface components give users a sense of

familiarity .

˃ Learn new applications more quickly and use them more

productively. 3

https://www.facebook.com/Oxus20

Page 4: JAVA GUI PART I

Introduction (cont..)

» Built from GUI components.

˃ Sometimes called controls or widgets

» User interacts via the mouse, the keyboard or another

form of input, such as voice recognition.

» IDEs

˃ Provide GUI design tools to specify a component's exact size and location in a visual

manner by using the mouse.

˃ Generates the GUI code for you.

˃ Greatly simplifies creating GUIs, but each IDE has different capabilities and generates

different code.

4

https://www.facebook.com/Oxus20

Page 5: JAVA GUI PART I

Simple GUI-Based Input / Output with JOptionPane

» Most applications use windows or dialog boxes (also called

dialogs) to interact with the user.

» JOptionPane (javax.swing package) provides prebuilt dialog

boxes for input and output

˃ Displayed via static JOptionPane methods.

» Next Example uses two input dialogs to get input from the

user and a message dialog to display the sum of the given

input. 5

https://www.facebook.com/Oxus20

Page 6: JAVA GUI PART I

Simple GUI example import javax.swing.JOptionPane;

public class Multiplier {

public static void main(String[] args) {

String firstNumber = JOptionPane.showInputDialog("Enter first number");

String secondNumber = JOptionPane.showInputDialog("Enter Second number");

int number1 = Integer.parseInt(firstNumber);

int number2 = Integer.parseInt(secondNumber);

int result = number1 * number2;

JOptionPane.showMessageDialog(null, "The Multiply of " + number1

+ " and " +number2 + " is " + result, "Result", JOptionPane.PLAIN_MESSAGE);

}

} 6

https://www.facebook.com/Oxus20

Page 7: JAVA GUI PART I

7

https://www.facebook.com/Oxus20

Preview

Page 8: JAVA GUI PART I

8

https://www.facebook.com/Oxus20

More on GUI "AWT and Swing"

8

Page 9: JAVA GUI PART I

AWT Vs. Swing

» When Java was first released in 1995, it contained a GUI API

referred to as the Abstract Windowing Toolkit (AWT).

» The classes and interfaces of the AWT are in the java.awt

package.

» Aware of the need for a more robust API for creating GUI

applications, Sun Microsystems teamed together with

Netscape (and other industry partners) then created Swing

package.

9

https://www.facebook.com/Oxus20

Page 10: JAVA GUI PART I

AWT Vs. Swing (cont.)

» Swing is actually a part of the Java Foundation Classes

(JFC).

» The classes and interfaces of Swing are found in the

javax.swing package.

» AWT components are referred to as heavyweight

components because their implementation relies

heavily on the underlying Operating System. 10

https://www.facebook.com/Oxus20

Page 11: JAVA GUI PART I

AWT Vs. Swing (cont.) » The look and feel of AWT components depend on the platform

the program is running on.

˃ For example, an AWT button will look like a Windows button when the program is run on a Windows

platform.

» Swing components are referred to as lightweight components

because their implementation does not rely on the underlying

Operating System.

» Because Swing components are lightweight, their appearance is

determined by you, the programmer, and not by which platform

the program is running. 11

https://www.facebook.com/Oxus20

Page 12: JAVA GUI PART I

Converting Frame to JFrame

» The names of the Swing classes all begin with a capital

"J" , like JButton, JLabel, JFrame.

» For the most part, an AWT program can be converted

to a Swing program by adding a capital J to the class

names used in the source code and recompiling the

code.

12

https://www.facebook.com/Oxus20

Page 13: JAVA GUI PART I

Creating Windows » The basic starting point of a GUI is the container because you need a

container before you can start laying out your components.

» The java.awt.Frame and javax.swing.JFrame classes are containers that

represent a basic window with a title bar and common windowing capabilities

such as resizing, minimizing, maximizing, and closing.

» When working with JFrame objects, there are basically three steps involved to

get a JFrame window to appear on the screen:

˃ Instantiate the JFrame object in memory.

˃ Give the JFrame object a size using setSize(), setBounds(), or pack().

˃ Make the Frame appear on the screen by invoking setVisible(true). 13

https://www.facebook.com/Oxus20

Page 14: JAVA GUI PART I

JFrame and Frame Constructors The java.awt.Frame class has four constructors:

» public Frame() or JFrame()

˃ Creates a new frame with no message in the title bar.

» public Frame(String title) or JFrame(String title)

˃ Creates a new frame with the given String appearing in the title bar.

» public Frame(GraphicsConfiguration gc) or

JFrame(GraphicsConfiguration gc)

˃ Creates a frame with the specified GraphicsConfiguration of a screen device.

» public Frame(String title, GraphicsConfiguration gc) or JFrame(String

title, GraphicsConfiguration gc)

˃ Creates a frame with the specified title and GraphicsConfiguration. 14

https://www.facebook.com/Oxus20

Page 15: JAVA GUI PART I

javax.swing.JFrame Class » The javax.swing.JFrame class represents a window similar to

Frame, except that JFrame adds support for the Swing

component architecture.

» However, a JFrame is different in terms of how components are

added to the Frame.

» A JFrame has three panes that components can be added to:

˃ a content pane

˃ a glass pane

˃ and a root pane.

» Typically, the content pane will contain all of the components of

the JFrame. 15

https://www.facebook.com/Oxus20

Page 16: JAVA GUI PART I

Simple Example of JFrame import javax.swing.JFrame;

public class GUIDemo {

public static void main(String[] args) {

JFrame window = new JFrame(); //create frame in memory

window.setTitle( "Oxus20" ); // Set tittle to frame

window.setSize(300, 300); // Set Size to frame

window.setLocationRelativeTo(null); //Set location to center of monitor

//let program to close

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setVisible(true); // Let frame to be visible on screen

}

} 16

https://www.facebook.com/Oxus20

Page 17: JAVA GUI PART I

END

https://www.facebook.com/Oxus20

17