Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7-3 (Book Chapter 14)

Similar presentations


Presentation on theme: "Chapter 7-3 (Book Chapter 14)"— Presentation transcript:

1 Chapter 7-3 (Book Chapter 14)
Introduction to OOP with Java 4th Ed, C. Thomas Wu Chapter 7-3 (Book Chapter 14) GUI and Event-Driven Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. © The McGraw-Hill Companies, Inc.

2 Intro to OOP with Java, C. Thomas Wu
Layout Managers The layout manager determines how the GUI components are added to the container (such as the content pane of a frame) Among the many different layout managers, the common ones are FlowLayout (see Ch14FlowLayoutSample.java) BorderLayout (see Ch14BorderLayoutSample.java) GridLayout (see Ch14GridLayoutSample.java) ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

3 Intro to OOP with Java, C. Thomas Wu
FlowLayout In using this layout, GUI components are placed in left-to-right order. When the component does not fit on the same line, left-to-right placement continues on the next line. As a default, components on each line are centered, but you can change it to left or right alignment. When the frame containing the component is resized, the placement of components is adjusted accordingly. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

4 Intro to OOP with Java, C. Thomas Wu
FlowLayout Sample This shows the placement of five buttons by using FlowLayout. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

5 FlowLayout ... contentPane = getContentPane( ); contentPane.setBackground( Color.WHITE ); contentPane.setLayout(new FlowLayout()); button1 = new JButton("button 1"); button2 = new JButton("button 2"); button3 = new JButton("button 3"); button4 = new JButton("button 4"); button5 = new JButton("button 5"); contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); contentPane.add(button4); contentPane.add(button5); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

6 FlowLayout FlowLayout - Left Alignment FlowLayout - Right Alignment
To change the center alignment: contentPane.setLayout(new FlowLayout(FlowLayout.LEFT)); contentPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); FlowLayout - Left Alignment FlowLayout - Right Alignment ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

7 Intro to OOP with Java, C. Thomas Wu
BorderLayout This layout manager divides the container into five regions: center, north, south, east, and west. If the window is enlarged, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space. Not all regions have to be occupied. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

8 Intro to OOP with Java, C. Thomas Wu
BorderLayout Sample contentPane.setLayout(new BorderLayout()); contentPane.add(button1, BorderLayout.NORTH); contentPane.add(button2, BorderLayout.SOUTH); contentPane.add(button3, BorderLayout.EAST); contentPane.add(button4, BorderLayout.WEST); contentPane.add(button5, BorderLayout.CENTER); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

9 BorderLayout ... contentPane = getContentPane( ); contentPane.setBackground( Color.WHITE ); contentPane.setLayout(new BorderLayout()); button1 = new JButton("button 1"); button2 = new JButton("button 2"); button3 = new JButton("button 3"); button4 = new JButton("button 4"); button5 = new JButton("button 5"); contentPane.add(button1, BorderLayout.NORTH); contentPane.add(button2, BorderLayout.SOUTH); contentPane.add(button3, BorderLayout.EAST); contentPane.add(button4, BorderLayout.WEST); contentPane.add(button5, BorderLayout.CENTER); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

10 Intro to OOP with Java, C. Thomas Wu
GridLayout This layout manager placesGUI components on equal-size N by M grids. Components are placed in top-to-bottom, left-to-right order. The number of rows and columns remains the same after the frame is resized, but the width and height of each region will change. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

11 Intro to OOP with Java, C. Thomas Wu
GridLayout Sample ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

12 GridLayout ... contentPane = getContentPane( ); contentPane.setBackground( Color.WHITE ); contentPane.setLayout(new GridLayout(2,3)); button1 = new JButton("button 1"); button2 = new JButton("button 2"); button3 = new JButton("button 3"); button4 = new JButton("button 4"); button5 = new JButton("button 5"); contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); contentPane.add(button4); contentPane.add(button5); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

13 Absolute Positioning Here we do not use any layout manager.
We place the GUI objects on the frame’s content pane by explicitly specifying their position and size. This is called absolute positioning ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

14 Absolute Positioning ... private static final int BUTTON_WIDTH = 80; private static final int BUTTON_HEIGHT = 30; contentPane.setLayout( null ); contentPane.setBackground( Color.WHITE ); //create and place a button on the frame's content pane okButton = new JButton("OK"); okButton.setBounds(70, 125, BUTTON_WIDTH, BUTTON_HEIGHT); contentPane.add(okButton); 125 30 70 80 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

15 Other Common GUI Components
Intro to OOP with Java, C. Thomas Wu Other Common GUI Components JCheckBox Useful in representing a collection of binary (yes/no, true/false) options. A JCheckBox object generates action events (like a JButton), but also generates item events, when the state of a check-box button changes (selected or deselected). see Ch14JCheckBoxSample1.java and Ch14JCheckBoxSample2.java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

16 Other Common GUI Components
JRadioButton The radio button is similar to the check-box, but only one button can be selected from the group. When you select a new item, the currently selected radio button will get deselected. Thus, the JRadioButton must be added to a group. A JRadioButton generates both action events and item events. see Ch14JRadioButtonSample.java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

17 Other Common GUI Components
JComboBox (drop-down list) Similar to JRadioButton, but the choices are presented to the user in a form of a drop-down list See Ch14JComboBoxSample.java Jlist Used to display a list of items You can highlight one or more selection see Ch14JListSample.java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

18 Intro to OOP with Java, C. Thomas Wu
Menus The javax.swing package contains three menu-related classes: JMenuBar, JMenu, and JMenuItem. JMenuBar is a bar where the menus are placed. There is one menu bar per frame. JMenu (such as File or Edit) is a group of menu choices. JMenuBar may include many JMenu objects. JMenuItem (such as Copy, Cut, or Paste) is an individual menu choice in a JMenu object. Only the JMenuItem objects generate events. Almost all nontrivial GUI programs support menus. By using these three menu-related classes, we can easily add menus to our Java programs. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

19 Intro to OOP with Java, C. Thomas Wu
Menu Components Edit View Help JMenuBar Edit View Help File JMenu The diagram shows how the menu-related objects correspond to the actual menus. JMenuItem separator ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

20 Sequence for Creating Menus
Intro to OOP with Java, C. Thomas Wu Sequence for Creating Menus Create a JMenuBar object and attach it to a frame. Create a JMenu object. Create JMenuItem objects and add them to the JMenu object. Attach the JMenu object to the JMenuBar object. This is not the only valid sequence. Other sequences are possible. We list this sequence as one possible sequence you can follow in creating menus. see Ch14MenueFrame.java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

21 Intro to OOP with Java, C. Thomas Wu
Handling Mouse Events Mouse events include such user interactions as moving the mouse dragging the mouse (moving the mouse while the mouse button is being pressed) clicking the mouse buttons. The MouseListener interface handles mouse button mouseClicked, mouseEntered, mouseExited, mousePressed, and mouseReleased The MouseMotionListener interface handles mouse movement mouseDragged and mouseMoved. - See Ch14TrackMouseFrame.java and Ch14SketchPad.java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

22 Java Online Tutorial More information about Java GUI can be found here: General Java information can be found here ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.


Download ppt "Chapter 7-3 (Book Chapter 14)"

Similar presentations


Ads by Google