Objectives Java Foundation Classes First GUI Program JFrame Structure JLabel JPanel Buttons and Borders Lesson Summary / Labs Topic List Java JFC, Swing, and JFrames2
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 http://download.oracle.com/javase/ tutorial/uiswing/start/about.html Objectives Java JFC, Swing, and JFrames3
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 Java JFC, Swing, and JFrames4
Java Foundation Classes Java JFC, Swing, and JFrames5 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.
Java Foundation Classes Java JFC, Swing, and JFrames6 JFC primary features include: Swing GUI components Pluggable Look-and-Feel Support Accessibility API Java 2D API Internationalization
Swing GUI components Java JFC, Swing, and JFrames7 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.
Pluggable Look-and-Feel Support Java JFC, Swing, and JFrames8 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.
Pluggable Look-and-Feel Support Java JFC, Swing, and JFrames9 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.
Accessibility API Java JFC, Swing, and JFrames10 Enables assistive technologies, such as screen readers and Braille displays, to get information from the user interface.
Java 2D API Java JFC, Swing, and JFrames11 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.
Internationalization Java JFC, Swing, and JFrames12 Allows developers to build applications that can interact with users worldwide in their own languages and cultural conventions.
Internationalization Java JFC, Swing, and JFrames13 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.
Swing components Java JFC, Swing, and JFrames14 The Swing API is powerful, flexible and immense. The Swing API has 18 public packages: javax.accessibilityjavax.swing.plafjavax.swing.text javax.swingjavax.swing.plaf.basicjavax.swing.text.html javax.swing.borderjavax.swing.plaf.metaljavax.swing.text.html.parser javax.swing.colorchooserjavax.swing.plaf.multijavax.swing.text.rtf javax.swing.eventjavax.swing.plaf.synthjavax.swing.tree javax.swing.filechooserjavax.swing.tablejavax.swing.undo
Swing components Java JFC, Swing, and JFrames15 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)
Java AWT Java JFC, Swing, and JFrames16 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….
First GUI program Java JFC, Swing, and JFrames17 Lets start with something simple…a Hello world in GUI! Here it is…
First GUI program Java JFC, Swing, and JFrames18 This 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.
First GUI program Java JFC, Swing, and JFrames19 The JOptionPane class also provides a simple input mechanism using the showInputDialog feature.
First GUI program Java JFC, Swing, and JFrames20 …which can retrieve information that can be used in another dialog box, or however you need to use it.
First GUI program Java JFC, Swing, and JFrames21 There is a bit more to explain about using these two dialog boxes, but for now this will suffice for our purposes. Well set them aside for now, and proceed to JFrames.
First GUI program Java JFC, Swing, and JFrames22 Since 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.
First GUI program Java JFC, Swing, and JFrames23 To 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.
First GUI program Java JFC, Swing, and JFrames24 To 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.
First GUI program Java JFC, Swing, and JFrames25 Unfortunately, this little JFrame window still is not impressive. It needs more, dont you think? How about a title? A name, perhaps? Lets do it! What do you think? Better? No?
First GUI program Java JFC, Swing, and JFrames26 Hmmm…name doesnt show up…perhaps if we stretch out the window a bit. There it is!
First GUI program Java JFC, Swing, and JFrames27 Perhaps we could control the size so the title shows up immediately without having to stretch it out? Hmmm…bigger, but still no title showing.
First GUI program Java JFC, Swing, and JFrames28 There it is! Now the window is wide enough. The setSize command sets the width and height of the JFrame.
First GUI program Java JFC, Swing, and JFrames29 Now, lets 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).
First GUI program Java JFC, Swing, and JFrames30 Changing it to (100,100) using the setLocation method causes this to happen!
First GUI program Java JFC, Swing, and JFrames31 There 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.
First GUI program Java JFC, Swing, and JFrames32 Lets look for a moment at the DOS window that shows up each time. Notice that normal console output shows up in it. Youve 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.
JFrame structure Java JFC, Swing, and JFrames33 Before we proceed any further, lets 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.
JLabel Java JFC, Swing, and JFrames34 One 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.
JLabel Java JFC, Swing, and JFrames35 Well, thats 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!
JLabel Java JFC, Swing, and JFrames36 Now, lets try to add another JLabel. Hmmm…the first one disappeared. Looks like this town aint big enough for both of us, reckon? There must be a better way, and there is!
JPanel Java JFC, Swing, and JFrames37 First add both labels to another component, the JPanel. Now they both show up!
JPanel Java JFC, Swing, and JFrames38 You 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.
JPanel Java JFC, Swing, and JFrames39 To enable icon inclusion, you must use the ImageIcon class as shown.
Buttons and Borders Java JFC, Swing, and JFrames40 Now lets add some cool stuff…buttons and borders. Check out the code and the result…study them carefully!
Buttons and Borders Java JFC, Swing, and JFrames41 The 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 doesnt do anything. Well get to that later.
Radio Button and Check Box Java JFC, Swing, and JFrames42 Here weve 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.
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 Java JFC, Swing, and JFrames43
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 http://download.oracle.com/javase/ tutorial/uiswing/start/about.html Lesson Summary Java JFC, Swing, and JFrames44
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 Java JFC, Swing, and JFrames45
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 Java JFC, Swing, and JFrames46
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 Java JFC, Swing, and JFrames47
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! Java JFC, Swing, and JFrames48