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-3 ( 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-3 ( 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-3 ( Book Chapter 14) GUI and Event-Driven Programming

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 2 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)

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 3 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.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 4 FlowLayout Sample This shows the placement of five buttons by using FlowLayout.

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. Chapter 14 - 5

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

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 7 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.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 8 BorderLayout Sample

9 BorderLayout ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 9... 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);...

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 10 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.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 11 GridLayout Sample

12 GridLayout ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 12... 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);...

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. Chapter 14 - 13

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); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 14 70 125 80 30

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 15 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

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. Chapter 14 - 16

17 Other Common GUI Components ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 17 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

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 18 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.

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 19 Menu Components EditViewHelp JMenuBar EditViewHelpFile JMenu JMenuItem separator

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 20 Sequence for Creating Menus 1.Create a JMenuBar object and attach it to a frame. 2.Create a JMenu object. 3.Create JMenuItem objects and add them to the JMenu object. 4.Attach the JMenu object to the JMenuBar object. –see Ch14MenueFrame.java

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 21 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

22 Java Online Tutorial More information about Java GUI can be found here: http://docs.oracle.com/javase/tutorial/uiswing/index.h tml General Java information can be found here http://docs.oracle.com/javase/tutorial/index.html ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 22


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

Similar presentations


Ads by Google