1 / 338

1 2

1 2. GUI Components: Part 1. OBJECTIVES. In this chapter you will learn: The design principles of graphical user interfaces (GUIs). To build GUIs and handle events generated by user interactions with GUIs.

bivory
Download Presentation

1 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 12 • GUI Components: Part 1

  2. OBJECTIVES In this chapter you will learn: • The design principles of graphical user interfaces (GUIs). • To build GUIs and handle events generated by user interactions with GUIs. • To understand the packages containing GUI components, event-handling classes and interfaces. • To create and manipulate buttons, labels, lists, text fields and panels. • To handle mouse events and keyboard events. • To use layout managers to arrange GUI components

  3. 11.1 Introduction • 11.2 Simple GUI-Based Input/Output with JOptionPane • 11.3 Overview of Swing Components • 11.4 Displaying Text and Images in a Window • 11.5 Text Fields and an Introduction to Event Handling with Nested Classes • 11.6 Common GUI Event Types and Listener Interfaces • 11.7 How Event Handling Works • 11.8JButton • 11.9 Buttons That Maintain State • 11.9.1JCheckBox • 11.9.2 JRadioButton • 11.10JComboBox and Using an Anonymous Inner Class for Event Handling

  4. 11.11JList • 11.12 Multiple-Selection Lists • 11.13 Mouse Event Handling • 11.14 Adapter Classes • 11.15JPanel Sublcass for Drawing with the Mouse • 11.16 Key-Event Handling • 11.17 Layout Managers • 11.17.1FlowLayout • 11.17.2BorderLayout • 11.17.3GridLayout • 11.18 Using Panels to Manage More Complex Layouts • 11.19JTextArea • 11.20 Wrap-Up

  5. 11.1 Introduction • Graphical user interface (GUI) • Presents a user-friendly mechanism for interacting with an application • Often contains title bar, menu bar containing menus, buttons and combo boxes • Built from GUI components

  6. button menus title bar menu bar combo box scroll bars Fig. 11.1 | Internet Explorer window with GUI components.

  7. 11.2 Simple GUI-Based Input/Output with JOptionPane • Dialog boxes • Used by applications to interact with the user • Provided by Java’s JOptionPane class • Contains input dialogs and message dialogs • invoking static JOptionPane methods

  8. Example: JOptionPane import javax.swing.JOptionPane; // program uses JOptionPane public class Addition { public static void main( String args[]) { // obtain user input from JOptionPane input dialogs String firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); String secondNumber = JOptionPane.showInputDialog( "Enter second integer" ); // convert String inputs to int values for use in a calculation int number1 = Integer.parseInt( firstNumber ); int number2 = Integer.parseInt( secondNumber ); int sum = number1 + number2; // add numbers

  9. Example (cont.) // display result in a JOptionPane message dialog JOptionPane.showMessageDialog(null,"The sum is " + sum,"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE ); } // end method main } // end class Addition

  10. Outline • Addition.java • (1 of 2)

  11. Input dialog displayed by lines 10–11 Outline Prompt to the user Text field in which the user types a value When the user clicks OK, showInputDialogreturns to the program the 100 typed by the user as a String. The program must convert the String to an int • Addition.java • (2 of 2) Input dialog displayed by lines 12–13 title bar Message dialog displayed by lines 22–23 When the user clicks OK, the message dialog is dismissed (removed from the screen)

  12. 11.2 Simple GUI-Based Input/Output with JOptionPane • Input dialogs • return String • Converting String to int values • Integer class static parseInt method • converts its String argument to an int value • Message Dialogs • JOptionPane’s static method showMessageDialog • 1th argumenet – possition – null: center of screen • 2th argument - message to be display • 3th argument – String on the Title bar of the dialog box • 4th argument – constants:icon types

  13. Fig. 11.3 | JOptionPanestatic constants for message dialogs.

  14. 11.3 Overview of Swing Components • Swing GUI components • Declared in package javax.swing • Most are pure Java components • Part of the Java Foundation Classes (JFC)

  15. Fig. 11.4 | Some basic GUI components.

  16. Swing vs. AWT • Abstract Window Toolkit (AWT) • Precursor to Swing • Declared in package java.awt • Does not provide consistent, cross-platform look-and-feel

  17. Portability Tip 11.1 • Swing components are implemented in Java, so they are more portable and flexible than the original Java GUI components from package java.awt, which were based on the GUI components of the underlying platform. For this reason, Swing GUI components are generally preferred.

  18. Lightweight vs. Heavyweight GUI Components • Lightweight components • Not tied directly to GUI components supported by underlying platform • Heavyweight components • Tied directly to the local platform • AWT components • Some Swing components

  19. Look-and-Feel Observation 11.4 • The look and feel of a GUI defined with heavyweight GUI components from package java.awtmay vary across platforms. Because heavyweight components are tied to the local-platform GUI, the look and feel varies from platform to platform.

  20. Superclasses of Swing’s Lightweight GUI Components • Class Component (package java.awt) • Subclass of Object • Declares many behaviors and attributes common to GUI components • Class Container (package java.awt) • Subclass of Component • Organizes Components • Class JComponent (package javax.swing) • Subclass of Container • Superclass of all lightweight Swing components

  21. Software Engineering Observation 11.1 • Study the attributes and behaviors of the classes in the class hierarchy of Fig. 11.5. These classes declare the features that are common to most Swing components.

  22. Fig. 11.5 | Common superclasses of many of the Swing components.

  23. Superclasses of Swing’s Lightweight GUI Components • Common lightweight component features • Pluggable look-and-feel to customize the appearance of components • Shortcut keys (called mnemonics) • Common event-handling capabilities • Brief description of component’s purpose (called tool tips) • Support for localization

  24. 11.4 Displaying Text and Images in a Window • Class JFrame • Most windows are an instance or subclass of this class • Provides title bar • Provides buttons to minimize, maximize and close the application

  25. import javax.swing.*; public class FrameTest { public static void main(String args[]) { JFrame firstFrame = new JFrame("First Frame"); firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); firstFrame.setSize(200, 300); firstFrame.setVisible(true); } // end main } // end class FrameTest

  26. Output an empty JFrame

  27. JFrame firstFrame = new JFrame("First Frame"); • firstFrame: reference of type JFrame • in javax.swing package • create JFrame object by setting its title • JFrame indirect subclass of java.awt.Window • attributes and behavior of a window: • Title bar at the top • buttoms for closing, minimizing ...

  28. Creating and Displaying a LabelFrame Window • Other JFrame methods • setDefaultCloseOperation • Dictates how the application reacts when the user clicks the close button • setSize • Specifies the width and height of the window • setVisible • Determines whether the window is displayed (true) or not (false)

  29. Labeling GUI Components • Label • Text instructions or information stating the purpose of each component • Created with class JLabel

  30. General Framework for GUI Applications • A subclass of JFrame • illustrate new GUI concepts • An application class • main creates and displays the applicatins window

  31. Look-and-Feel Observation 11.5 • Text in a JLabel normally uses sentence-style capitalization.

  32. Adding a label to the Frame class Label1 import javax.swing.*; import java.awt.*; public class Label0 { public static void main(String[] args){ LabelFrame firstLabel = new LabelFrame("Frame with Label"); firstLabel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); firstLabel.setSize(200, 300); firstLabel.setVisible(true); } // end main } // end class Label1

  33. class LabelFrame extends JFrame { // creates a JLabel reference private JLabel label1; public LabelFrame(String title) { super(title); // send the title to JFrame’s consturctor // set labout setLayout(new FlowLayout()); // create a new JLabel refered by label1 label1 = new JLabel("I am a label"); add(label1); // add to the frame } // end constructor } // end classLabelFrame

  34. Class Label0 – test or application class LabelFrame firstLabel = new LabelFrame("Frame with Label"); • LabelFrame extends from JFrame • firstLabel: is a reference to a LabelFrame type object • Create the object with its title • use methods of JFrame to set • close operation • size • visiblilility • of the firstLabel

  35. Class LabelFrame - extends from JFrame • hold the components – label1 • set the layout • create the labels label1 • add to the JFrame

  36. Another approach – not prefered • Create a JFrame object send title to its constrcutor set the properties create a JLabel reference set layout on JFrame create the object label1 add label1 to jlabelFrame

  37. import javax.swing.*; import java.awt.*; public class Label0 { public static void main(String[] args){ JFrame firstLabel = new JFrame("Frame with Label");firstLabel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); firstLabel.setSize(200, 300); firstLabel.setVisible(true); JLabel label1; // a JLabel type varible firstLabel. setLayout(new FlowLayout()); // create a new JLabel refered by label1 label1 = new JLabel("I am a label"); firstLabel.add(label1); // add to the frame } // end main } // end class Label1

  38. Adding a labels to the Frame class Label1 import javax.swing.*; import java.awt.*; public class Label1 { public static void main(String[] args) { LabelFrame firstLabel = new LabelFrame("Frame with Label"); firstLabel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); firstLabel.setSize(200, 300); firstLabel.setVisible(true); } // end main } // end class Label1

  39. Explanations LabelFrame firstLabel = new LabelFrame("Frame with Label"); • LabelFrame extends from JFrame • firstLabel: is a reference to a LabelFrame type object • Create the object with its title • use methods of JFrame to set • close operation • size • visiblilility • of the firstLabel

  40. class Label1Frame • extends from JFrame • hold the components – 3 JLabel s • set the layout • create the labels label1 and label2 • seting a tooltip for label2 – toolTipSet method • add to the JFrame

  41. class Label1Freme class LabelFrame extends JFrame { // creates three JLabel references private JLabel label1; private JLabel label2; private JLabel label3; public LabelFrame(String title) { super(title); // send the title to JFrame’s consturctor // set labout setLayout(new FlowLayout());

  42. // create a new JLabel refered by label1 label1 = new JLabel("I am a label"); add(label1); // add to the frame // create a new JLabel refered by label2 label2 = new JLabel("second label"); label2.setToolTipText("tooltip of scond label"); // set a tooltip to label2 invoking setToolTip method add(label2); // add to the frame

  43. // create a new JLabel refered by label3 label3 = new JLabel(); label3.setText("label 3"); label3.setToolTipText("tool tip text 3"); label3.setHorizontalTextPosition(SwingConstants.CENTER); label3.setVerticalTextPosition(SwingConstants.BOTTOM); add(label3);; } // end constructor } // end class LabelFrame

  44. Specifying the Layout • Laying out containers • Determines where components are placed in the container • Window creatred with JFrame • Done in Java with layout managers • One of which is class FlowLayout • Set with the setLayout method • İnherited indirectly form class Container • setLayout(new FlowLayout()); • A FlowLayout object is created without a reference and send to setLayout method as an argument • The parameter lacal variable is the refernce – LayoutManager interface

  45. output of program

  46. Outline • LabelFrame.java • (1 of 2)

  47. Outline • LabelFrame.java • (1 of 2)

  48. Outline • LabelFrame.java • (2 of 2)

  49. Outline • LabelTest.java

  50. Outline • LabelTest.java

More Related