Upload
julius-florence
View
228
Download
1
Tags:
Embed Size (px)
Citation preview
JFrame Lesson One
Java JFC , Swing, and JFrames
OWEN COMPUTER SCIENCE LESSONS
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
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
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
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.
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
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.
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.
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.
10Java JFC , Swing, and JFrames
Accessibility API
• Enables assistive technologies, such as screen readers and Braille displays, to get information from the user interface.
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.
12Java JFC , Swing, and JFrames
Internationalization
• Allows developers to build applications that can interact with users worldwide in their own languages and cultural conventions.
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.
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
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)
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….
17Java JFC , Swing, and JFrames
First GUI program
Let’s start with something simple…a “Hello world” in GUI!
Here it is…
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.
19Java JFC , Swing, and JFrames
First GUI programThe JOptionPane class also provides a simple input mechanism using the showInputDialog feature.
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.
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.
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.
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.
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.
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?
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!
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.
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.
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).
30Java JFC , Swing, and JFrames
First GUI programChanging it to (100,100) using the setLocation method causes this to happen!
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.
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.
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.
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.
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!
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!
37Java JFC , Swing, and JFrames
JPanelFirst add both labels to another component, the JPanel.
Now they both show up!
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.
39Java JFC , Swing, and JFrames
JPanelTo enable icon inclusion, you must use the ImageIcon class as shown.
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!
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.
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.
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
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
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
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
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
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!