1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 17 - Maria Litvin, Gary Litvin

32 20 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Slide 1

  • Objectives:

  • Pluggable Look and Feel

  • PLAF (cont’d)

  • GUI Components

  • GUI Events

  • Listeners

  • Listeners (cont’d)

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • GUI Components Review

  • JLabel

  • JButton

  • JCheckBox

  • etc.

  • Layouts

  • Layouts (cont’d)

  • FlowLayout

  • FlowLayout (cont’d)

  • GridLayout

  • GridLayout (cont’d)

  • BorderLayout

  • BorderLayout (cont’d)

  • BoxLayout

  • BoxLayout (cont’d)

  • Slide 28

  • Default Layouts

  • Menus

  • Review:

  • Review (cont’d):

Nội dung

Chapter 17 - GUI components and events. This chapter is only a brief overview of Java’s Swing package and event handling classes. After studying this chapter, the reader should be able to get enough background to research on his or her own the remaining classes and methods from Java’s API docs.

Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin   Chapter:  17 GUI Components and Events Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved Objectives: • Get a more systematic introduction to basic Swing components, their methods, and events they generate • Components discussed:      JLabel JButton JToggleButton JCheckBox JComboBox     JSlider JTextField JPasswordField JTextArea 17­2 Pluggable Look and Feel • Look and feel (LAF) refers to the GUI aspect of a program • Java’s Swing supports PLAF (Pluggable Look and Feel) • Java provides several LAFs, including:      “Metal” platform independent “Nimbus” a more polished, cross-platform LAF, added in Java “Windows” for Windows “Windows Classic” like Windows 95 “Motif” for Unix / Linux 17­3 PLAF (cont’d) • Three ways to set look and feel:  swing.properties file: swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel  Command-line switch: C:\> java -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel  In the program: try { UIManager.setLookAndFeel(plafName); } catch (Exception ex) { } 17­4 GUI Components • Components are created using constructors: JLabel guest = new JLabel ("Guest”); • To be usable, a component must be added to the application’s “content pane” or to another component: JPanel scorePanel = new JPanel(); scorePanel.add (guest); 17­5 GUI Events • Components (except JLabel) can generate events • Events are captured and processed by “listeners” — objects equipped to handle a particular type of events • Different types of events are processed by different types of listeners 17­6 Listeners • Different types of “listeners” are described as interfaces:     ActionListener ChangeListener ItemListener etc • The same object can serve as different listeners (as long as its class implements all the corresponding interfaces) 17­7 Listeners (cont’d) Objects of this class are GoHandlers but also ActionListeners public class GoHandler implements ActionListener { public void actionPerformed (ActionEvent e) { This method is called automatically when the } button is clicked } JButton go = new JButton (“Go”); go.addActionListener (new GoHandler ()); This method expects an ActionListener; a GoHandler object qualifies 17­8 Listeners (cont’d) • When implementing an event listener, programmers often use a private inner class that has access to all the fields of the surrounding public class • An inner class can have constructors and private fields, like any other class • A private inner class is accessible only in its outer class 17­9 Listeners (cont’d) go is accessible public class MyPanel extends JPanel in constructors { and methods of private JButton go; the inner class go = new JButton (“Go”); go.addActionListener (new GoHandler ()); private class GoHandler implements ActionListener { public void actionPerformed (ActionEvent e) { go.setText(“Stop”); } } } 17­10 Layouts • A layout manager is a strategy for placing components on the content pane or another component (usually a panel) • In Java, the content pane and any GUI component is a Container • A layout is chosen by calling the container’s setLayout method 17­18 Layouts (cont’d) • Layouts are used to achieve some degree of platform independence and scalability • awt/Swing support several layout managers Here we consider four:     FlowLayout GridLayout BorderLayout BoxLayout • Each of these classes implements the java.awt.LayoutManager interface 17­19 FlowLayout • Places components in a line as long as they fit, then starts the next line • Uses “best judgement” in spacing components Centers by default • Lets each component assume its natural (preferred) size • Often used for placing buttons on panels 17­20 FlowLayout (cont’d) Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add (new JButton ("Back to Start")); c.add (new JButton ("Previous Slide")); c.add (new JButton ("Next Slide")); c.add (new JButton ("Last Slide")); c.add (new JButton ("Exit")); 17­21 GridLayout • Splits the panel into a grid with given numbers of rows and columns • Places components into the grid cells • Forces the size of each component to occupy the whole cell • Allows additional spacing between cells 17­22 GridLayout (cont’d) Container c = getContentPane(); c.setLayout (new GridLayout(3, 2, 10, 20 )); c.add (new JButton ("Back to Start")); c.add (new JButton ("Previous Slide")); c.add (new JButton ("Next Slide")); Extra space c.add (new JButton ("Last Slide")); between the c.add (new JButton ("Exit")); cells (in pixels) 17­23 BorderLayout • Divides the area into five regions and adds a component to the specified region NORTH WEST CENTER EAST SOUTH • Forces the size of each component to occupy the whole region 17­24 BorderLayout (cont’d) Container c = getContentPane(); c.setLayout(new BorderLayout()); // optional: default c.add (new JButton ("Next Slide"), BorderLayout.EAST); c.add (new JButton ("Previous Slide"), BorderLayout.WEST); c.add (new JButton ("Back to Start"), BorderLayout.NORTH); c.add (new JButton ("Last Slide"), BorderLayout.SOUTH); c.add (new JButton ("Exit"), BorderLayout.CENTER); 17­25 BoxLayout • In a horizontal box, components are placed horizontally, left to right • In a vertical box, components are placed vertically, top to bottom “Horizontal” or “vertical” has nothing to with the shape of the box itself 17­26 BoxLayout (cont’d) • BoxLayout is the default layout for a Box container • The idiom for working with boxes is slightly different: Box box1 = Box.createHorizontalBox(); box1.add ( ); // add a spacer, 60 pixels: box1.add(Box.createHorizontalStrut (60)); Box box2 = Box.createVerticalBox(); 17­27 BoxLayout (cont’d) Container c = getContentPane(); c.setLayout(new FlowLayout()); Box box = Box.createVerticalBox(); box.add (new JButton ("Next Slide")); box.add (new JButton ("Previous Slide")); box.add (Box.createVerticalStrut (20) ); box.add (new JButton ("Exit")); c.add (box); Adds extra vertical space between components 17­28 Default Layouts • Each component has a default layout manager, which remains in effect until the component’s setLayout method is called • The defaults are: Content pane JPanel Box BorderLayout FlowLayout BoxLayout 17­29 Menus • You can add a JMenuBar object to JFrame or JApplet • You can add JMenu objects to a JMenuBar • You can add other JMenus, JMenuItems, JCheckBoxMenuItems, JRadioButtonMenuItems, etc to a JMenu • See Section 17.5 for an example 17­30 Review: • • • • What are the three ways to set a PLAF? Can a container contain another container? Name several Swing GUI components Is an action listener a class, an interface, an object, or a method? • How FlowLayout and GridLayout deal with the sizes of components? 17­31 Review (cont’d): • What is the default layout manager for the content pane? • What type of objects can you add to a JMenu? 17­32 ... swing.defaultlaf=com.sun .java. swing.plaf.windows.WindowsLookAndFeel  Command-line switch: C:> java -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel  In the program: try { UIManager.setLookAndFeel(plafName);... b.isSelected()) } } 17? ?16 etc See Java Methods Appendix C 17? ?17 Layouts • A layout manager is a strategy for placing components on the content pane or another component (usually a panel) • In Java, the... JTextArea 17? ?2 Pluggable Look and Feel • Look and feel (LAF) refers to the GUI aspect of a program • Java? ??s Swing supports PLAF (Pluggable Look and Feel) • Java provides several LAFs, including: 

Ngày đăng: 04/11/2020, 23:17