1. Trang chủ
  2. » Thể loại khác

Java - profthinh ď jhtp5_13

94 227 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

  • Chapter 13 - Graphical User Interface Components: Part 1

  • Slide 2

  • 13.1 Introduction

  • Fig. 13.1 Netscape window with GUI components

  • Fig. 13.2 Some basic GUI components

  • 13.2 Overview of Swing Components

  • Slide 7

  • Fig. 13.3 Common superclasses of many of the Swing components

  • 13.3 JLabel

  • LabelTest.java Line 8 Line 20 Line 21

  • LabelTest.java Lines 16-17 Lines 32-37

  • LabelTest.java

  • 13.4 Event Handling

  • Fig. 13.5 Some event classes of package java.awt.event

  • Slide 15

  • Fig. 13.6 Event-listener interfaces of package java.awt.event

  • 13.5 TextFields

  • TextFieldTest.java Lines 8-9 Line 20 Line 24

  • TextFieldTest.java Line 30 Line 34 Lines 39-42

  • TextFieldTest.java Line 56 Line 59

  • TextFieldTest.java

  • Slide 22

  • 13.6 How Event Handling Works

  • Fig. 13.8 Event registration for JTextField textField1

  • 13.7 JButton

  • Fig. 13.9 Swing button hierarchy

  • ButtonTest.java Line 8 Line 20 Lines 24-26

  • ButtonTest.java Line 31 Lines 32-33 Line 50

  • ButtonTest.java

  • 13.8 JCheckBox and JRadioButton

  • CheckBoxTest.java Line 9 Line 22

  • CheckBoxTest.java Lines 26 and 29 Lines 34-35

  • CheckBoxTest.java Line 54 Line 65

  • CheckBoxTest.java

  • RadioButtonTest.java Lines 10-11 Line 12

  • RadioButtonTest.java Lines 28-35 Lines 41-45

  • RadioButtonTest.java Lines 55-60

  • RadioButtonTest.java Line 83 Line 85

  • 13.9 JComboBox

  • ComboBoxTest.java

  • ComboBoxTest.java Lines 27-28 Line 29 Line 34 Lines 38-39

  • Slide 42

  • 13.10 JList

  • ListTest.java

  • ListTest.java Line 30 Line 34 Line 38 Line 43 Lines 45-46

  • Slide 46

  • 13.11 Multiple-Selection Lists

  • MultipleSelectionTest.java Lines 10-12 and 24 Lines 26-27

  • MultipleSelectionTest.java Line 40 Lines 54-55

  • MultipleSelectionTest.java

  • 13.12 Mouse Event Handling

  • Fig. 13.16 MouseListener and MouseMotionListener interface methods

  • MouseTracker.java Lines 20-21

  • MouseTracker.java Line 29 Line 36 Line 43 Line 50

  • MouseTracker.java Line 58 Line 66 Line 73

  • MouseTracker.java

  • 13.13 Adapter Classes

  • Fig. 13.18 Event-adapter classes and the interfaces they implement in package java.awt.event

  • Painter.java Line 22

  • Painter.java Line 27 Line 30 Line 51

  • Painter.java

  • MouseDetails.java Line 15

  • MouseDetails.java Line 41 Lines 43-44 Line 46 Line 48 Line 51

  • MouseDetails.java

  • Fig. 13.21 InputEvent methods that help distinguish among left-, center- and right-mouse-button clicks

  • 13.14 Key Event Handling

  • KeyDemo.java Line 23

  • KeyDemo.java Line 31 Line 33 and 40 Line 38 Line 45

  • KeyDemo.java Line 57

  • KeyDemo.java

  • 13.15 Layout Managers

  • Fig. 13.23 Layout managers

  • 13.15.1 FlowLayout

  • FlowLayoutDemo.java Lines 17 and 21

  • FlowLayoutDemo.java Line 33 Line 53

  • FlowLayoutDemo.java Line 71

  • FlowLayoutDemo.java

  • 13.15.2 BorderLayout

  • BorderLayoutDemo.java Lines 18 and 22

  • BorderLayoutDemo.java Lines 33-37 Lines 50 and 52

  • BorderLayoutDemo.java

  • Slide 82

  • 13.15.3 GridLayout

  • GridLayoutDemo.java Line 21 Line 22

  • GridLayoutDemo.java Lines 46 and 48

  • GridLayoutDemo.java

  • 13.16 Panels

  • PanelDemo.java Line 23

  • PanelDemo.java Line 29 Line 32

  • 13.17 (Optional Case Study) Thinking About Objects: Use Cases

  • Slide 91

  • Slide 92

  • Fig. 13.28 Use case diagram for elevator simulation from user’s perspective

  • Fig. 13.29 Use case diagram from the perspective of a Person

Nội dung

Java - profthinh ď jhtp5_13 tài liệu, giáo án, bài giảng , luận văn, luận án, đồ án, bài tập lớn về tất cả các lĩnh vực...

Chapter 13 - Graphical User Interface Components: Part Outline 13.1 13.2 13.3 13.4 13.5 13.6 13.7 13.8 13.9 13.10 13.11 13.12 13.13 13.14 Introduction Overview of Swing Components JLabel Event Handling TextFields How Event Handling Works JButton JCheckBox and JRadioButton JComboBox JList Multiple-Selection Lists Mouse Event Handling Adapter Classes Key Event Handling 2003 Prentice Hall, Inc All rights reserved Chapter 13 - Graphical User Interface Components: Part Outline 13.15 Layout Managers 13.15.1 FlowLayout 13.15.2 BorderLayout 13.15.3 GridLayout 13.16 Panels 13.17 (Optional Case Study) Thinking About Objects: Use Cases  2003 Prentice Hall, Inc All rights reserved 13.1 Introduction • Graphical User Interface (GUI) – Gives program distinctive “look” and “feel” – Provides users with basic level of familiarity – Built from GUI components (controls, widgets, etc.) • User interacts with GUI component via mouse, keyboard, etc  2003 Prentice Hall, Inc All rights reserved Fig 13.1 button menus Netscape window with GUI components menu bar combo box scroll bars  2003 Prentice Hall, Inc All rights reserved Fig 13.2 Some basic GUI components Component JLabel Description JTextField An area in which the user inputs data from the keyboard The area can also display information JButton An area that triggers an event when clicked with the mouse JCheckBox A GUI component that is either selected or not selected JComboBox A drop-down list of items from which the user can make a selection by clicking an item in the list or possibly by typing into the box JList An area containing a list of items from which the user can make a selection by clicking on any element in the list Multiple elements can be selected JPanel A container in which components can be placed and organized An area where uneditable text or icons can be displayed  2003 Prentice Hall, Inc All rights reserved 13.2 Overview of Swing Components • Swing GUI components – Package javax.swing – Components originate from AWT (package java.awt) – Contain look and feel • Appearance and how users interact with program – Lightweight components • Written completely in Java  2003 Prentice Hall, Inc All rights reserved 13.2 Overview of Swing Components • Class Component – Contains method paint for drawing Component onscreen • Class Container – Collection of related components – Contains method add for adding components • Class JComponent – Pluggable look and feel for customizing look and feel – Shortcut keys (mnemonics) – Common event-handling capabilities  2003 Prentice Hall, Inc All rights reserved Fig 13.3 Common superclasses of many of the Swing components Object Object Component Component Container Container JComponent JComponent  2003 Prentice Hall, Inc All rights reserved 13.3 JLabel • Label – Provide text on GUI – Defined with class JLabel – Can display: • Single line of read-only text • Image • Text and image  2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Outline // Fig 13.4: LabelTest.java // Demonstrating the JLabel class import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LabelTest extends JFrame { private JLabel label1, label2, label3; // set up GUI public LabelTest() { super( "Testing JLabel" ); // get content pane and set its layout Container container = getContentPane(); container.setLayout( new FlowLayout() ); // JLabel constructor with a string argument label1 = new JLabel( "Label with text" ); label1.setToolTipText( "This is label1" ); container.add( label1 ); LabelTest.java Line Declare three JLabels Line 20 Line 21 Create first JLabel with text “Label with text” Tool tip is text that appears when user moves cursor over JLabel  2003 Prentice Hall, Inc All rights reserved 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 for ( int count = 0; count < names.length; count++ ) { buttons[ count ] = new JButton( names[ count ] ); buttons[ count ].addActionListener( this ); } // place buttons in BorderLayout; order not important container.add( buttons[ ], BorderLayout.NORTH ); container.add( buttons[ ], BorderLayout.SOUTH ); container.add( buttons[ ], BorderLayout.EAST ); container.add( buttons[ ], BorderLayout.WEST ); container.add( buttons[ ], BorderLayout.CENTER ); Outline BorderLayoutDem o.java Lines 33-37 Place JButtons in regions specified by BorderLayout Lines 50 and 52 setSize( 300, 200 ); setVisible( true ); } // end constructor BorderLayoutDemo // handle button events public void actionPerformed( ActionEvent event ) { for ( int count = 0; count < buttons.length; count++ ) if ( event.getSource() == buttons[ count ] ) buttons[ count ].setVisible( false ); else buttons[ count ].setVisible( true ); When JButtons are “invisible,” they are not displayed on screen, and BorderLayout rearranges  2003 Prentice Hall, Inc All rights reserved 53 54 55 56 57 58 59 60 61 62 63 64 // re-layout the content pane layout.layoutContainer( getContentPane() ); } public static void main( String args[] ) { BorderLayoutDemo application = new BorderLayoutDemo(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } Outline BorderLayoutDem o.java } // end class BorderLayoutDemo  2003 Prentice Hall, Inc All rights reserved Outline BorderLayoutDem o.java  2003 Prentice Hall, Inc All rights reserved 83 13.15.3 GridLayout • GridLayout – Divides container into grid of specified row an columns – Components are added starting at top-left cell • Proceed left-to-fight until row is full  2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Outline // Fig 13.26: GridLayoutDemo.java // Demonstrating GridLayout import java.awt.*; import java.awt.event.*; import javax.swing.*; GridLayoutDemo java public class GridLayoutDemo extends JFrame implements ActionListener { private JButton buttons[]; private final String names[] = { "one", "two", "three", "four", "five", "six" }; private boolean toggle = true; private Container container; private GridLayout grid1, grid2; // set up GUI public GridLayoutDemo() { super( "GridLayout Demo" ); // set up layouts grid1 = new GridLayout( 2, 3, 5, ); grid2 = new GridLayout( 3, ); // get content pane and set its layout container = getContentPane(); container.setLayout( grid1 ); Line 21 Line 22 Create GridLayout grid1 with rows and columns Create GridLayout grid2 with rows and columns  2003 Prentice Hall, Inc All rights reserved 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 // create and add buttons buttons = new JButton[ names.length ]; for ( int count = buttons[ count buttons[ count container.add( } 0; count < names.length; count++ ) { ] = new JButton( names[ count ] ); ].addActionListener( this ); buttons[ count ] ); Outline GridLayoutDemo java Lines 46 and 48 setSize( 300, 150 ); setVisible( true ); } // end constructor GridLayoutDemo // handle button events by toggling between layouts public void actionPerformed( ActionEvent event ) { Toggle current if ( toggle ) GridLayout when container.setLayout( grid2 ); else user presses JButton container.setLayout( grid1 ); toggle = !toggle; // set toggle to opposite value container.validate(); }  2003 Prentice Hall, Inc All rights reserved 53 54 55 56 57 58 59 60 public static void main( String args[] ) { GridLayoutDemo application = new GridLayoutDemo(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } Outline GridLayoutDemo java } // end class GridLayoutDemo  2003 Prentice Hall, Inc All rights reserved 87 13.16 Panels • Panel – Helps organize components – Class JPanel is JComponent subclass – May have components (and other panels) added to them  2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig 13.27: PanelDemo.java // Using a JPanel to help lay out components import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PanelDemo extends JFrame { private JPanel buttonPanel; private JButton buttons[]; Outline PanelDemo.java Line 23 // set up GUI public PanelDemo() { super( "Panel Demo" ); // get content pane Container container = getContentPane(); // create buttons array buttons = new JButton[ ]; // set up panel and set its layout buttonPanel = new JPanel(); Create JPanel buttonPanel.setLayout( new GridLayout( 1, buttons.length ) ); to hold JButtons  2003 Prentice Hall, Inc All rights reserved 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 // create and add buttons for ( int count = 0; count < buttons.length; count++ ) { buttons[ count ] = new JButton( "Button " + ( count + ) ); buttonPanel.add( buttons[ count ] ); Add } JButtons to JPanel PanelDemo.java Line 29 container.add( buttonPanel, BorderLayout.SOUTH ); setSize( 425, 150 ); setVisible( true ); Outline Line 32 Add JPanel to SOUTH region of Container } // end constructor PanelDemo public static void main( String args[] ) { PanelDemo application = new PanelDemo(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class PanelDemo  2003 Prentice Hall, Inc All rights reserved 13.17 (Optional Case Study) Thinking About Objects: Use Cases • Use case – Represents capabilities that systems provide to clients • Automated-teller-machine use cases – “Deposit Money,” “Withdraw Money,” “Transfer Funds”  2003 Prentice Hall, Inc All rights reserved 90 13.17 (Optional Case Study) Thinking About Objects: Use Cases • Use-case diagram – Models use cases in system – Facilitates system-requirements gathering – Notation • Stick figure represents actor – Actor represents set of roles that external entity can play • System box (rectangle) contains system use cases • Ovals represent use cases  2003 Prentice Hall, Inc All rights reserved 91 13.17 (Optional Case Study) Thinking About Objects: Use Cases • Elevator-simulation use cases – “Create Person” • From user’s perspective – “Relocate Person” (move to other floor) • From Person’s perspective • Constructing GUI – Use “Create Person” use case  2003 Prentice Hall, Inc All rights reserved 92 Fig 13.28 Use case diagram for elevator simulation from user’s perspective Create Person User  2003 Prentice Hall, Inc All rights reserved 93 Fig 13.29 Use case diagram from the perspective of a Person Relocate Person Person  2003 Prentice Hall, Inc All rights reserved 94 ... ButtonTest .java // Creating JButtons import java. awt.*; import java. awt.event.*; import javax.swing.*; public class ButtonTest extends JFrame { private JButton plainButton, fancyButton; ButtonTest .java. .. 20 21 22 23 24 25 26 // Fig 13.7: TextFieldTest .java // Demonstrating the JTextField class import java. awt.*; import java. awt.event.*; import javax.swing.*; public class TextFieldTest extends... 19 20 21 22 23 24 Outline // Fig 13.11: CheckBoxTest .java // Creating JCheckBox buttons import java. awt.*; import java. awt.event.*; import javax.swing.*; public class CheckBoxTest extends JFrame

Ngày đăng: 11/12/2017, 19:45

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w