jframe lesson one java jfc, swing, and jframes by john b. owen all rights reserved ©2011

48
JFrame Lesson One Java JFC , Swing, and JFrames OWEN COMPUTER SCIENCE LESSONS By John B. Owen All rights reserved ©2011

Upload: julius-florence

Post on 30-Mar-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

JFrame Lesson One

Java JFC , Swing, and JFrames

OWEN COMPUTER SCIENCE LESSONS

By John B. Owen

All rights reserved

©2011

Page 2: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

2Java JFC , Swing, and JFrames

• Objectives

• Java Foundation Classes

• First GUI Program

• JFrame Structure

• JLabel

• JPanel

• Buttons and Borders

• Lesson Summary / Labs

Topic List

Page 3: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

3Java JFC , Swing, and JFrames

• Students will understand how to use JOptionPane dialog boxes and create simple JFrames for GUI based applications.

• This lesson is adapted in part from the Oracle Training website at http://download.oracle.com/javase/tutorial/uiswing/start/about.html

Objectives

Page 4: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

4Java JFC , Swing, and JFrames

• This lesson will BARELY scratch the surface of what is possible using these tools.

• The serious student is highly encouraged to visit the website listed previously, and delve into the vast and rich array of tools presented there in order to further the GUI development skills required for a significant application.

Objectives

Page 5: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

5Java JFC , Swing, and JFrames

Java Foundation Classes

• First, an overview, straight from the Oracle website…

• JFC is short for Java Foundation Classes, which encompass a group of features for building graphical user interfaces (GUIs) and adding rich graphics functionality and interactivity to Java applications.

Page 6: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

6Java JFC , Swing, and JFrames

Java Foundation Classes

• JFC primary features include:• Swing GUI components• Pluggable Look-and-Feel

Support• Accessibility API• Java 2D API• Internationalization

Page 7: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

7Java JFC , Swing, and JFrames

Swing GUI components

• Includes everything from buttons to split panes to tables.

• Many components are capable of sorting, printing, and drag and drop, to name a few of the supported features.

Page 8: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

8Java JFC , Swing, and JFrames

Pluggable Look-and-Feel Support

• The look and feel of Swing applications is pluggable, allowing a choice of look and feel.

• For example, the same program can use either the Java or the Windows look and feel.

Page 9: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

9Java JFC , Swing, and JFrames

Pluggable Look-and-Feel Support

• Additionally, the Java platform supports the GTK+ look and feel, which makes hundreds of existing look and feels available to Swing programs.

• Many more look-and-feel packages are available from various sources.

Page 10: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

10Java JFC , Swing, and JFrames

Accessibility API

• Enables assistive technologies, such as screen readers and Braille displays, to get information from the user interface.

Page 11: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

11Java JFC , Swing, and JFrames

Java 2D API

• Enables developers to easily incorporate high-quality 2D graphics, text, and images in applications and applets.

• Java 2D includes extensive APIs for generating and sending high-quality output to printing devices.

Page 12: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

12Java JFC , Swing, and JFrames

Internationalization

• Allows developers to build applications that can interact with users worldwide in their own languages and cultural conventions.

Page 13: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

13Java JFC , Swing, and JFrames

Internationalization

• With the input method framework developers can build applications that accept text in languages that use thousands of different characters, such as Japanese, Chinese, or Korean.

Page 14: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

14Java JFC , Swing, and JFrames

Swing components

• The Swing API is powerful, flexible — and immense. The Swing API has 18 public packages: javax.accessibility javax.swing.plaf javax.swing.text

javax.swing javax.swing.plaf.basic javax.swing.text.html

javax.swing.border javax.swing.plaf.metal

javax.swing.text.html.parser

javax.swing.colorchooser

javax.swing.plaf.multi javax.swing.text.rtf

javax.swing.event javax.swing.plaf.synth javax.swing.tree

javax.swing.filechooser javax.swing.table javax.swing.undo

Page 15: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

15Java JFC , Swing, and JFrames

Swing components

Fortunately, most programs use only a small subset of the API. Most of the code in this lesson uses only one or two Swing packages:

• javax.swing

• javax.swing.event (not always required)

Page 16: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

16Java JFC , Swing, and JFrames

Java AWT

We will also be using the Abstract Windowing Toolkit, which contains all of the classes for creating user interfaces and for painting graphics and images, like changing color, drawing circles and rectangles, setting up menus, drop down boxes, etc….

Page 17: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

17Java JFC , Swing, and JFrames

First GUI program

Let’s start with something simple…a “Hello world” in GUI!

Here it is…

Page 18: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

18Java JFC , Swing, and JFrames

First GUI programThis is a special feature called a JOptionPane.showMessageDialog box.

It allows you easily to create a simple message to the user, inside a predefined frame, with several possible features.

Page 19: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

19Java JFC , Swing, and JFrames

First GUI programThe JOptionPane class also provides a simple input mechanism using the showInputDialog feature.

Page 20: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

20Java JFC , Swing, and JFrames

First GUI program…which can retrieve information that can be used in another dialog box, or however you need to use it.

Page 21: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

21Java JFC , Swing, and JFrames

First GUI programThere is a bit more to explain about using these two dialog boxes, but for now this will suffice for our purposes.

We’ll set them aside for now, and proceed to JFrames.

Page 22: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

22Java JFC , Swing, and JFrames

First GUI programSince the HelloWorldSwing class extends the JFrame class, we can construct an object, compile it, and execute it.

However, not much shows up…just a black window with a “Press any key…” message. Not very impressive.

Page 23: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

23Java JFC , Swing, and JFrames

First GUI programTo actually see the object, you must use the setVisible command.

A little better, but still not too impressive.

Also, when you click out of it, the accompanying black box provides no “Press any key…” option…not good.

Page 24: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

24Java JFC , Swing, and JFrames

First GUI programTo fix that problem, you must include this command :

me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

…which brings back the “Press any key…” message on exiting the window.

Page 25: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

25Java JFC , Swing, and JFrames

First GUI programUnfortunately, this little JFrame window still is not impressive. It needs more, don’t you think?

How about a title? A name, perhaps? Let’s do it!

What do you think? Better? No?

Page 26: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

26Java JFC , Swing, and JFrames

First GUI programHmmm…name doesn’t show up…perhaps if we stretch out the window a bit.

There it is!

Page 27: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

27Java JFC , Swing, and JFrames

First GUI programPerhaps we could control the size so the title shows up immediately without having to stretch it out?

Hmmm…bigger, but still no title showing.

Page 28: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

28Java JFC , Swing, and JFrames

First GUI programThere it is! Now the window is wide enough.

The setSize command sets the width and height of the JFrame.

Page 29: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

29Java JFC , Swing, and JFrames

First GUI programNow, let’s try to control where it first opens up.

Up to this point, the JFrame default location is at the top left corner of the desktop…location (0,0).

Page 30: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

30Java JFC , Swing, and JFrames

First GUI programChanging it to (100,100) using the setLocation method causes this to happen!

Page 31: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

31Java JFC , Swing, and JFrames

First GUI programThere is a command, setBounds, that combines these two control aspects of location and size. The first two parameters indicate the location, and the next two, the size.

Page 32: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

32Java JFC , Swing, and JFrames

First GUI programLet’s look for a moment at the DOS window that shows up each time. Notice that normal console output shows up in it.

You’ve probably seen this, like with PC^2, a popular network-based contest submission program.

Each client window has a DOS platform on which it runs.

Page 33: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

33Java JFC , Swing, and JFrames

JFrame structureBefore we proceed any further, let’s talk about the underlying structure of a JFrame. It is one of three TOP LEVEL CONTAINERS that are used in GUI applications. The other two are JApplet and JDialog.

Below is the basic structure of the JFrame class. It is derived from the Frame class, which extends the Window class.

It has a Menu Bar and a Content Pane, into which all other components are placed.

Page 34: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

34Java JFC , Swing, and JFrames

JLabelOne of the components that can be added to the Content Pane is the JLabel, an object created using a String.

To add the JLabel, you must first get the content pane, then add components to it.

Page 35: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

35Java JFC , Swing, and JFrames

JLabelWell, that’s not really necessary after all.

JFrame has overridden the add method so that it will automatically add a component to the content pane, even without the “getContentPane” method call.

Makes it a lot easier!

Page 36: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

36Java JFC , Swing, and JFrames

JLabelNow, let’s try to add another JLabel.

Hmmm…the first one disappeared.

“Looks like this town ain’t big enough for both of us, reckon?”

There must be a better way, and there is!

Page 37: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

37Java JFC , Swing, and JFrames

JPanelFirst add both labels to another component, the JPanel.

Now they both show up!

Page 38: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

38Java JFC , Swing, and JFrames

JPanelYou can even use a gif file when creating a JLabel, as you see here.

The gif file must accessible to the Java class, best located in the same folder, or close enough to make a path that is easy to include.

Page 39: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

39Java JFC , Swing, and JFrames

JPanelTo enable icon inclusion, you must use the ImageIcon class as shown.

Page 40: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

40Java JFC , Swing, and JFrames

Buttons and BordersNow let’s add some cool stuff…buttons and borders.

Check out the code and the result…study them carefully!

Page 41: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

41Java JFC , Swing, and JFrames

Buttons and BordersThe border of the JPanel is a beveled border, colored red and yellow.

A JButton was added, also with a beveled border, colored blue and green. It clicks, but doesn’t do anything. We’ll get to that later.

Page 42: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

42Java JFC , Swing, and JFrames

Radio Button and Check BoxHere we’ve added a radio button and a check box.

The size of the window was expanded to 400X300 to accommodate all of the components.

The check box “checks” and the radio button “clicks”, but there is still no functionality…more on that in the next lesson.

Page 43: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

43Java JFC , Swing, and JFrames

• In this lesson you have learned the basics of creating JOptionPane dialog boxes as well as creating a simple JFrame that contains various components, such as panels, buttons, boxes and borders.

• In the next lesson we will learn how to add functionality to all of these!

Lesson Summary

Page 44: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

44Java JFC , Swing, and JFrames

• As was pointed out at the beginning of this lesson, to do some serious GUI application development takes MUCH MORE than was presented here.

• Go to this website to see the whole picture…it is massive! http://download.oracle.com/javase/tutorial/uiswing/start/about.html

Lesson Summary

Page 45: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

45Java JFC , Swing, and JFrames

• For this lab, create a JFrame of your own, using all of the techniques you learned in this lesson.

• The JFrame must be in the center of the screen, large enough to contain all of the components without having to manually stretch out the frame.

• See specific requirements on the next slide.

JFrame Lab 1A

Page 46: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

46Java JFC , Swing, and JFrames

• Requirements:• Set the title to your full name.• One JPanel to contain all of the

components, with a colored beveled border.

• At least two instances each of:• A String JLabel • An icon JLabel • A JButton with a unique label• A JCheckBox with a unique label• A JRadioButton with a unique label

JFrame Lab 1A

Page 47: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

47Java JFC , Swing, and JFrames

• Using the JOptionPane message and input dialogs, create a program to do a Madlibs story, asking the user for at least five inputs, and then putting them into a silly or interesting story.

• Use the dialogs appropriately in producing this program.

• The final story must be in at least three dialog boxes, one right after the other.

JFrame Lab 1B

Page 48: JFrame Lesson One Java JFC, Swing, and JFrames By John B. Owen All rights reserved ©2011

48Java JFC , Swing, and JFrames

• You now understand some of the basics of creating GUI based applications.

• There is still much more to learn!

• In the next JFrame Lesson, you will learn how to add functionality to the buttons and boxes.

CONGRATULATIONS!