Java Programming, Second Edition - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Java Programming, Second Edition

Description:

to take advantage of the Swing GUI components and their methods ... JCheckBox Class- Consists of a JLabel positioned beside a square ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 25
Provided by: dwigh2
Category:

less

Transcript and Presenter's Notes

Title: Java Programming, Second Edition


1
Java Programming, Second Edition
  • Chapter Thirteen
  • Understanding Swing Components

2
In this chapter, you will
  • Use the JFrame class
  • Extend the JFrame class
  • Use the JPanel class
  • Understand Swing event listeners
  • Use the JCheckBox class

3
  • Use the ButtonGroup class
  • Use the JComboBox class
  • Create JScrollPanes

4
Using the JFrame Class
  • GUI Components
  • Insert the import statement import javax.swing.
    to take advantage of the Swing GUI components and
    their methods
  • Also called widgets, which stands for windows
    gadgets
  • Within the awt package, components are defined in
    the Component class
  • When you use components in a Java program, you
    usually place them in containers

5
Using the JFrame Class
  • Container- Type of component that holds other
    components so that you can treat a group of
    several components as a single entity
  • Usually takes the form of a window
  • Defined in the Container class

6
Using the JFrame
  • The JFrame class is a subclass of the awt
    Component class
  • The JFrame is the best container option for
    hosting Java applications
  • The setSize() method allows you to set the
    physical size of a JFrame
  • The setVisible() method makes the JFrame
    component visible or invisible
  • Create a JFrame so that you can place other
    objects within it for display using a JPanel

7
Using the JFrame
  • The JFrame class has four constructors
  • JFrame() constructs a new frame that is initially
    invisible
  • JFrame(GraphicsConfiguration gc) creates a JFrame
    in the specified GraphicsConfiguration of a
    screen device and a blank title
  • JFrame(String title) creates a new, initially
    invisible JFrame with the specified title
  • JFrame(String title, GraphicsConfiguration gc)
    creates a JFrame with the specified title and the
    specified GraphicsConfiguration of a screen

8
Using the JFrame Class
  • A JFrames action in response to a user clicking
    the Close button is set by passing an argument to
    the setDefaultCloseOperation() method placed
    inside the JFrame constructor method
  • The most common action is to close the
    application using the argument JFrame.EXIT_ON_CLOS
    E

9
Using the JFrame Class
  • EXIT_ON_CLOSE exists the program when the JFrame
    is closed
  • DISPOSE_ON_CLOSE closes the frame, disposes of
    the JFrame object, and keeps running the
    application
  • DO_NOTHING_ON_CLOSE keeps the JFrame and
    continues running
  • HIDE_ON_CLOSE closes the JFrame and continues
    running

10
(No Transcript)
11
Using the JPanel Class
  • The simplest Swing container is the JPanel
  • You add components to a JPanel using the add()
    method
  • Call the setContentPane() method with the panel
    object created to set the application's content
    pane

12
Understanding Swing Event Listeners
  • Classes that respond to user events must
    implement an interface that deals with the events
  • These interfaces are called event listeners
  • Each listener can handle a specific event type
  • A class can implement as many event listeners as
    needed

13
(No Transcript)
14
(No Transcript)
15
Using Swing Event Listeners
  • When a user event takes place, the appropriate
    method is automatically called by the system

16
Using the JCheckBox Class
  • JCheckBox Class- Consists of a JLabel positioned
    beside a square
  • You can click the square to display or remove a
    check mark

17
(No Transcript)
18
Using the ButtonGroup Class
  • ButtonGroup Class
  • Can group several JCheckBoxes so a user can
    select only one at a time
  • When you group JCheckBox objects, all other
    JCheckBoxes are automatically turned off when the
    user selects any one checkbox
  • Either create a ButtonGroup and then create the
    individual JCheckBoxes, or you can create the
    JCheckBoxes and then create the Button group
  • Similar to a set of radio buttons but more than
    one can be checked

19
Using the JComboBox Class
  • JComboBox class- For picking items from a list or
    entering text into a field

20
Using the JComboBox Class
  • You can build a JComboBox by using a constructor
    with no arguments and then adding items to the
    list with the addItem() method
  • The following statements create a JComboBox with
    three options
  • JComboBox majorChoice new
  • JComboBox()
  • majorChoice.addItem(English)
  • majorChoice.addItem(Math)
  • majorChoice.addItem(Sociology)

21
(No Transcript)
22
Creating JScrollPanes
  • A JScrollPane is a container whose methods can be
    used to hold any component that can be scrolled
  • Add a JTextArea component to use both multiple
    rows and columns

23
JScrollPane forms
  • The JScrollPane constructor takes one of four
    forms
  • JScrollPane() creates an empty JScrollPane where
    both horizontal and vertical scrollbars appear
    when needed
  • JScrollPane(Component) creates a JScrollPane that
    displays the contents of the specified component
  • JScrollPane(Component, int, int) creates a
    JScrollPane that displays the specified
    component, vertical scrollbar, and horizontal
    scrollbar.
  • JScrollPane(int, int) creates a scroll pane with
    specified vertical and horizontal scrollbars

24
Horizontal and vertical scrollbar constants
  • HORIZONTAL_SCROLLBAR_AS_NEEDED
  • HORIZONTAL_SCROLLBAR_ALWAYS
  • HORIZONTAL_SCROLLBAR_NEVER
  • VERTICAL_SCROLLBAR_AS_NEEDED
  • VERTICAL_SCROLLBAR_ALWAYS
  • VERTICAL_SCROLLBAR_NEVER
Write a Comment
User Comments (0)
About PowerShow.com