Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 1 Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 1 Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 1 Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming

2 Objectives After you have read and studied this chapter, you should be able to –Define a subclass of JFrame to implement a customized frame window. –Write event-driven programs using Java's delegation-based event model –Write GUI application programs using JButton, JLabel, ImageIcon, JTextField, and JTextArea. –Understand GUI application programs with menus –Understand GUI application programs that process mouse events

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 3 Graphical User Interface In Java, GUI-based programs are implemented by using classes from the javax.swing and java.awt packages. The Swing classes provide greater compatibility across different operating systems. They are fully implemented in Java, and behave the same on different operating systems.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 4 Sample GUI Objects Various GUI objects from the javax.swing package.

5 JOptionPane Using the JOptionPane class is a simple way to display the result of a computation to the user or receive an input from the user. We use the showMessageDialog class method for output. If we pass null as the first argument, the dialog appears on the center of the screen. If we pass a frame object as the first argument, the dialog is positioned at the center of the frame. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 5

6 Using JOptionPane for Output ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 6 import javax.swing.*;... JOptionPane.showMessageDialog( null, “I Love Java” ); import javax.swing.*;... JOptionPane.showMessageDialog( null, “I Love Java” );

7 Using JOptionPane for Output - 2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 7 import javax.swing.*;... JOptionPane.showMessageDialog( null, “one\ntwo\nthree” ); //place newline \n to display multiple lines of output import javax.swing.*;... JOptionPane.showMessageDialog( null, “one\ntwo\nthree” ); //place newline \n to display multiple lines of output

8 Using JOptionPane for Output - 3 import javax.swing.*; class Ch14ShowMessageDialog { public static void main(String[] args) { JFrame jFrame; jFrame = new JFrame( ); jFrame.setSize(400,300); jFrame.setVisible(true); JOptionPane.showMessageDialog(jFrame, "How are you?"); JOptionPane.showMessageDialog(null, "Good Bye"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 8

9 Using JOptionPane for Output - 3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 9 After Pressing OK or closing the “How are you?” dialog, the “Good Bye” dialog appears

10 JOptionPane for Input-1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 10 import javax.swing.*;... String inputstr = JOptionPane.showInputDialog( null, “What is your name?” ); import javax.swing.*;... String inputstr = JOptionPane.showInputDialog( null, “What is your name?” ); We use the showInputDialog class method for input. This method returns the input as a String value so we need to perform type conversion for input of other data types

11 JOptionPane for Input-2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 11 import javax.swing.*;... String inputstr = JOptionPane.showInputDialog( null, “Enter Age:” ); int age= Integer.parseInt(inputstr); import javax.swing.*;... String inputstr = JOptionPane.showInputDialog( null, “Enter Age:” ); int age= Integer.parseInt(inputstr);

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 12 Subclassing JFrame To create a customized frame window, we define a subclass of the JFrame class. The JFrame class contains rudimentary functionalities to support features found in any frame window.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 13 Creating a Plain JFrame import javax.swing.*; class Ch7DefaultJFrame { public static void main( String[] args ) { JFrame defaultJFrame; defaultJFrame = new JFrame(); defaultJFrame.setVisible(true); }

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 14 Creating a Subclass of JFrame To define a subclass of another class, we declare the subclass with the reserved word extends. import javax.swing.*; class Ch7JFrameSubclass1 extends JFrame {... }

15 Creating a Subclass of JFrame An instance of Ch14JFrameSubclass1 will have the following default characteristics: –The title is set to My First Subclass. –The program terminates when the close box is clicked. –The size of the frame is 300 pixels wide by 200 pixels high. –The frame is positioned at screen coordinate (150, 250). These properties are set inside the default constructor. Source File: Ch14JFrameSubclass1.java

16 Creating a Subclass of JFrame import javax.swing.*; class Ch14JFrameSubclass1 extends JFrame { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; public Ch14JFrameSubclass1( ) { //set the frame default properties setTitle ( "My First Subclass" ); setSize ( FRAME_WIDTH, FRAME_HEIGHT ); setLocation ( FRAME_X_ORIGIN, FRAME_Y_ORIGIN ); //register 'Exit upon closing' as a default close operation setDefaultCloseOperation( EXIT_ON_CLOSE ); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 16

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 17 Displaying Ch14JFrameSubclass1 Here's how a Ch14JFrameSubclass1 frame window will appear on the screen.

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 18 The Content Pane of a Frame The content pane is where we put GUI objects such as buttons, labels, scroll bars, and others. We access the content pane by calling the frame’s getContentPane method. This gray area is the content pane of this frame.

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 19 Changing the Background Color Here's how we can change the background color of a content pane to blue: Container contentPane = getContentPane(); contentPane.setBackground(Color.BLUE); Source File: Ch14JFrameSubclass2.java

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 20 Placing GUI Objects on a Frame There are two ways to put GUI objects on the content pane of a frame: –Use a layout manager FlowLayout BorderLayout GridLayout –Use absolute positioning null layout manager

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 21 Placing a Button-1 A JButton object a GUI component that represents a pushbutton. Here's an example of how we place a button with FlowLayout. FlowLayout places objects in the top-to-bottom, left-to right order. contentPane.setLayout( new FlowLayout()); okButton = new JButton("OK"); cancelButton = new JButton("CANCEL"); contentPane.add(okButton); contentPane.add(cancelButton);

22 Placing a Button-2 import javax.swing.*; import java.awt.*; class Ch14JButtonFrame extends JFrame { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; private JButton cancelButton; private JButton okButton; public static void main(String[] args) { Ch14JButtonFrame frame = new Ch14JButtonFrame(); frame.setVisible(true); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 22

23 Placing a Button-3 public Ch14JButtonFrame() { Container contentPane = getContentPane( ); contentPane.setLayout(new FlowLayout()); setSize ( FRAME_WIDTH, FRAME_HEIGHT ); setResizable ( false ); setTitle ( "Program Ch14JButtonFrame" ); setLocation ( FRAME_X_ORIGIN, FRAME_Y_ORIGIN ); //create and place two buttons on the frame's content pane okButton = new JButton("OK"); contentPane.add(okButton); cancelButton = new JButton("CANCEL"); contentPane.add(cancelButton); //register 'Exit upon closing' as a default close operation setDefaultCloseOperation( EXIT_ON_CLOSE ); } } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 23

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 24 Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism to process events is called event handling. Event handling is implemented by two types of objects: –event source objects –event listener objects

25 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 25 Event Source and Event Listeners Objects An event source is a GUI object where an event occurs. We say an event source generates events. Buttons, text boxes, list boxes, and menus are common event sources in GUI-based applications. An event listener object is an object that includes a method that gets executed in response to the generated events. A listener must be associated, or registered, to a source, so it can be notified when the source generates events.

26 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 26 Connecting Source and Listener JButton Handler event source event listener notify register A listener must be registered to a event source. Once registered, it will get notified when the event source generates events.

27 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 27 There are different event types and event listeners Mouse events are handled by mouse listeners Item selection events are handled by Item listeners and so forth Among the different types of events, the action event is the most common. –Clicking on a button generates an action event –Selecting a menu item generates an action event –and so forth Action events are generated by action event sources and handled by action event listeners. Event Types

28 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 28 Handling Action Events JButton Button Handler action event source action event listener actionPerformed addActionListener JButton button = new JButton("OK"); ButtonHandler handler = new ButtonHandler( ); button.addActionListener(handler); The class ButtonHandler is a user-defined event listener (event handler). It must implement the Interface ActionListener It must include the method actionPerformed The method actionPerformed includes the code we want to be executed in response to the generated events

29 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 29 ActionListener Interface When we call the addActionListener method of an event source, we must pass an instance of a class that implements the ActionListener interface. The ActionListener interface includes one method named actionPerformed. A class that implements the ActionListener interface must therefore provide the method body of actionPerformed. Since actionPerformed is the method that will be called when an action event is generated, this is the place where we put a code we want to be executed in response to the generated events.

30 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 30 The ButtonHandler Class

31 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 31 Container as Event Listener Instead of defining a separate event listener such as ButtonHandler, it is much more common to have an object that contains the event sources be a listener. –Example: We make this frame a listener of the action events of the buttons it contains. event source event listener

32 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 32 Ch14JButtonFrameHandler... class Ch14JButtonFrameHandler extends JFrame implements ActionListener {... public void actionPerformed(ActionEvent event) { JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); setTitle("You clicked " + buttonText); }

33 Ch14JButtonFrameHandler (complete program-1) import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14JButtonFrameHandler extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; private JButton cancelButton; private JButton okButton; public static void main(String[] args) { Ch14JButtonFrameHandler frame = new Ch14JButtonFrameHandler(); frame.setVisible(true); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 33

34 Ch14JButtonFrameHandler (complete program-2) public Ch14JButtonFrameHandler() { Container contentPane = getContentPane( ); setSize (FRAME_WIDTH, FRAME_HEIGHT); setResizable (false); setTitle ("Program Ch14JButtonFrameHandler"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane.setLayout(new FlowLayout()); okButton = new JButton("OK"); contentPane.add(okButton); cancelButton = new JButton("CANCEL"); contentPane.add(cancelButton); //registering this frame as an action listener of the two buttons cancelButton.addActionListener(this); okButton.addActionListener(this); setDefaultCloseOperation( EXIT_ON_CLOSE ); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 34

35 Ch14JButtonFrameHandler (complete program-3) public void actionPerformed(ActionEvent event) { JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); this.setTitle("You clicked " + buttonText); } } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 35


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 1 Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming."

Similar presentations


Ads by Google