©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14...

download ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 Advanced GUI

of 79

  • date post

    26-Mar-2015
  • Category

    Documents

  • view

    214
  • download

    0

Embed Size (px)

Transcript of ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14...

  • Slide 1

TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 Advanced GUI Slide 2 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 Objectives After you have read and studied this chapter, you should be able to Arrange GUI objects on a container, using layout managers and nested panels. Write GUI application programs that process mouse events. Understand how the SketchPad class introduced in Chapter 2 is implemented. Slide 3 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 Objectives After you have read and studied this chapter, you should be able to Write GUI application programs with JCheckBox, JRadioButton, JComboBox, JList, and JSlider objects. Develop programs using a variation of the model-view-controller (MVC) design pattern. Slide 4 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events Mouse events include such user interactions as moving the mouse, dragging the mouse, and clicking the mouse buttons. To study these events, we will define a subclass of JFrame that handles the left mouse button click events. The subclass of JFrame we will define is named Ch14TrackMouseFrame. Slide 5 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events A Ch14TrackMouseFrame object is an event source of mouse events. We will allow this object to be an event listener as well. Its class must therefore implement the MouseListener interface. Class Ch14TrackMouseFrame extends Frame implements MouseListener { } Slide 6 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events The MouseListener interface has five abstract methods: mouseClicked mouseEntered mouseExited mousePressed mouseReleased The argument to all five methods is an instance of MouseEvent. Slide 7 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events The mouseClicked method is called every time the left mouse button is clicked, or pressed down and released. The getX and getY methods of MouseEvent retrieve the x and y coordinate values of wherever the mouse is clicked. Slide 8 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events If we want to detect the mouse button press and release separately, we can provide a method body to the mousePressed and mouseReleased methods, respectively. Slide 9 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events The getClickCount method of MouseEvent will detect the number of mouse clicks performed, so different events may be triggered by a single click or double click, for example. Slide 10 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events Implementing the MouseMotionListener interface will allow us to track mouse dragging. The MouseMotionListener interface has two abstract methods: mouseDragged mouseMoved The argument to both methods is an instance of MouseEvent. Slide 11 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events When either the right or left mouse button is pressed, the event listeners mousePressed method is called. To determine which mouse button is pressed inside the mousePressed method, we call the isMetaDown method of MouseEvent. The isMetaDown method returns true if the right button is pressed. Slide 12 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events Similar to the mousePressed method, the mouseDragged method is called whether the mouse was dragged with the left or right button. The isMetaDown method may also be used here to determine which button was used to drag the mouse. Slide 13 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events We now have the tools to understand the implementation of the SketchPad class from Chapter 2. Following is the fully implemented program. Slide 14 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events /* File: Ch14SketchPad.java */ import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14SketchPad extends JFrame implements MouseListener, MouseMotionListener { private static final int FRAME_WIDTH = 450; private static final int FRAME_HEIGHT = 300; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; private int last_x; private int last_y; Slide 15 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events public static void main(String[] args) { Ch14SketchPad frame = new Ch14SketchPad(); frame.setVisible(true); } public Ch14SketchPad( ) { //set frame properties setTitle ("Chapter 14 SketchPad"); setSize ( FRAME_WIDTH, FRAME_HEIGHT ); setResizable( false ); setLocation ( FRAME_X_ORIGIN, FRAME_Y_ORIGIN ); setDefaultCloseOperation(EXIT_ON_CLOSE); last_x = last_y = 0; addMouseListener( this ); addMouseMotionListener( this ); } Slide 16 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events public void mousePressed( MouseEvent event ) { int x = event.getX(); int y = event.getY(); if ( event.isMetaDown() ) { //the right mouse button is pressed, so //erase the contents Graphics g = getGraphics(); Rectangle r = getBounds(); g.clearRect(0, 0, r.width, r.height); g.dispose(); } else { //the left mouse button is pressed, //remember the starting point of a new //mouse drag last_x = x; last_y = y; } Slide 17 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.1 Handling Mouse Events public void mouseClicked ( MouseEvent event ) { } public void mouseEntered ( MouseEvent event ) { } public void mouseExited ( MouseEvent event ) { } public void mouseReleased( MouseEvent event ) { } public void mouseDragged( MouseEvent event ) { int x = event.getX(); int y = event.getY(); if ( !event.isMetaDown() ) { //dont process the right button drag Graphics g = getGraphics(); g.drawLine(last_x, last_y, x, y); g.dispose(); last_x = x; last_y = y; } public void mouseMoved ( MouseEvent event ) { } } Slide 18 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels For building practical GUI-based Java programs, we must learn how to use layout managers effectively. We will begin by covering the three basic managers: FlowLayout BorderLayout GridLayout Slide 19 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels The default content pane of a frame is an instance of JPanel. We can place a JPanel inside another JPanel. Each of these nesting panels may be assigned a different layout manager. The capability of nesting panels with different layout managers presents opportunities for creating intricate layouts on a frame. Slide 20 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels The most basic layout is java.awt.FlowLayout. In this layout, GUI components are placed in left-to-right order. As a default, components on each line are centered. When the frame containing the component is resized, the placement of the components is adjusted accordingly. Slide 21 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels We first assign the desired layout manager to the container (in this case, the content pane of a frame) in the frames constructor. Container contentPane = getContentPane();... contentPane.setLayout(new FlowLayout()); A container has a default layout manager assigned to it, but it is safer to explicitly assign the desired layout manager ourselves. Slide 22 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels We then create five buttons and add them to the content pane. JButton button1, button2, button3, button4, button5;... button1 = new JButton(button1);... contentPane.add(button1);... Slide 23 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 14.1 Placement of five buttons by using FlowLayout when the frame is first opened and after the frame is resized. Slide 24 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels The second layout manager is java.awt.BorderLayout. This manager divides the container into five regions: Center North South East West Slide 25 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels We set the BorderLayout as contentPane.setLayout(new BorderLayout()); And place GUI components with the second argument specifying the region: contentPane.add(button1,BorderLayout.NORTH); contentPane.add(button1,BorderLayout.SOUTH); Slide 26 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 14.2 Placement of five buttons by using BorderLayout when the frame is first opened and after the frame is resized. Slide 27 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 14.3 Placement of two buttons by using BorderLayout. Buttons are placed on the center and east regions. Slide 28 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.2 Layout Managers and Panels The default of BorderLayout has no gaps between the regions. We can specify the amount of vertical and horizontal gaps between the regions in pixels. contentPane.setLayout(new BorderLayout(10, 20)); Slide 29 TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 14.