lesson 10 ui & events java oriented object programming i · java oriented object programing i...

13
Java Oriented Object Programming I UI & Events Lesson 10

Upload: others

Post on 21-Jan-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programming IUI & EventsLesson 10

Page 2: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Objectives

● Making UI with Swing○ jFrames○ jPanel○ jButton

● Customizing with Interfaces/ Inheritance○ Handle events○ Extend jPanel for drawing

● Eclipse Window Builder

Page 3: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Making a User Interface with SwingSwing is a toolkit designed to assist developers in creating Graphical User Interfaces (GUI) for Java programs.

Output

Step 1:

Create JFrame

The JFrame is the window used to house all the components used to create the GUI such as the title bar, control buttons.

● Open your editor and create a new Java project and a new class

Page 4: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Step 2

Create JPanel

JPanel is used to create a container within the window for Swing components

Output

Page 5: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Step 3

Create JButton

JButton is used to create buttons in swing

Output

Page 6: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Step 4

Event based programming

Generally theres a main loop that listens or waits for events and then triggers a callback function or a function assigned to that event.

There are many forms of listeners, in the following example an Action Listener is attached to the button.

Page 7: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Extending JPanel for Drawing

This is an example of when you would need to use inheritance to extend a class.

Its powerful and used to create games.

You cannot paint on a JPanel unless you extend the class.

Page 8: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Extending JFrame for Drawing Cont’d

Output

Page 9: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Eclipse WindowbuilderEclipse windowbuilder is a Graphical User Interface(GUI) designed to create Java applications simply without spending lots of time writing the code.

Step 1

Create Windowbuilder

In Eclipse select File → New → Other → Windowbuilder → Swing Designer → JFrame

Once you have created the Windowbuilder, the program generates the code needed for the window.

Notice the two tabs at the bottom, Source and Design. You will use these two tabs for development.

Page 10: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Windowbuilder Cont’d

On the last slide you saw the Source tab(where all your code is), here is the Design tab where you will create your window and Windowbuilder will automatically generate the code to the Source tab.

Begin creating your design by simply selecting the component and placing it on the window.

Page 11: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Windowbuilder Cont’d

Design window by choosing from the palette of options. This is where you will find Containers, Layouts, Components etc.

Once you select the item from the palette, click anywhere on the window where you would like the item to be positioned.

The components panel shows you the hierarchy of items added.

The properties panel is a quick and simple way to change any of the default settings. Eclipse will make the changes to the Source code automatically.

Page 12: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Java Oriented Object Programing IUI & Events

Windowbuilder Cont’d

Here is the code for the button that Eclipse automatically added to the Source tab. You can compare it to the properties panel to see the that it is the same information.

Page 13: Lesson 10 UI & Events Java Oriented Object Programming I · Java Oriented Object Programing I UI & Events Making a User Interface with Swing Swing is a toolkit designed to assist

Objectives

● Making UI with Swing○ jFrames○ jPanel○ jButton

● Customizing with Interfaces/ Inheritance○ Handle events○ Extend jPanel for drawing

● Eclipse Window Builder