CIS3931 - Intro to JAVA - PowerPoint PPT Presentation

About This Presentation
Title:

CIS3931 - Intro to JAVA

Description:

CIS3931 - Intro to JAVA. Lecture Notes Set 10. 28-June-05. GUI Programming ... Demos. Demo of completed Project 4 w/ code. Demo of completed Project 5 w/ code ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 17
Provided by: UniformOfD
Learn more at: http://www.cs.fsu.edu
Category:
Tags: java | cis3931 | demos | intro

less

Transcript and Presenter's Notes

Title: CIS3931 - Intro to JAVA


1
CIS3931 - Intro to JAVA
  • Lecture Notes Set 10
  • 28-June-05
  • GUI Programming Buttons and Actions.

2
In this set of notes
  • Adding a button to a frame
  • The content pane of a frame
  • A little on layout managers
  • Actions listeners
  • Registering a listener
  • Using actionPerformed()
  • Changing the background color of a frame

3
Buttons
  • The swing class that defines buttons is called
    JButton
  • JButton is a kind of container
  • Can contain other components
  • Think of it like a mini JFrame
  • Can add pictures to the button

4
Adding a button to a frame
  • Create the button by calling the new constructor
    on the button
  • JButton button new JButton(ClickMe)
  • Add the button to the content pane
  • getContentPane().add(button)

5
Button Example Code
  • See ButtonDemo.java

6
ContentPane
  • The content pane is a container that represents
    the main rectangle of the frame.
  • The add method of the content pane puts an object
    (or another container) into the frame.
  • The content pane is referenced via the
    getContentPane() method of the frame.
  • getContentPane().add(OBJECT/CONTAINER)

7
Layout Managers
  • Layout Manager decides where objects will go
    (and how big they will be) when they are added to
    a frame.
  • Sometimes the default choices arent always the
    best
  • If you dont specify one, a default layout
    manager is chosen.
  • ALWAYS choose the layout manager BEFORE you add
    components to the frame.

8
FlowLayout Manager
  • Part of the java.awt package.
  • Puts components into the frame row by row in the
    order they are added.
  • Automatically picks a reasonable size for the
    conponents

9
FlowLayout Manager
Note The layout of the frame will change if you
add components in a different order.
10
Setting the Layout Manager
  • import java.awt.
  • . . .
  • public class ButtonDemo extends JFrame
  • Button bChange //Create a button
  • ButtonDemo()
  • bChange new Button("Click Me!")
    //Initialize the button
  • // choose the layout manager
  • getContentPane().setLayout( new FlowLayout()
    )
  • getContentPane().add( bChange ) //Add the
    button
  • public static void main ( String args )
  • ButtonDemo frm new ButtonDemo()
  • . . . . .

11
Sample Program with Layout Manager
  • See LayoutDemo1.java

12
Adding an ActionListener() to a button
  • Must define complete class of a button listener
    from scratch (there is no built-in ButtonAdapter
    class)
  • Must implement ActionListener
  • ActionListener is an interface (not a class) that
    contains a single method
  • public void actionPerformed(ActionEvent evt)

13
ActionEvent
  • ActionEvent is an Event object that represents an
    event (such as a button click).
  • Contains information that can be used when
    responding to an event.
  • Class must implement ActionListener

14
ActionListener() example
  • Changing the background color with a button click
  • See ActionDemo1.java

15
An addition to the example
  • Allowing the button to change colors multiple
    times
  • See ActionDemo2.java

16
Demos
  • Demo of completed Project 4 w/ code
  • Demo of completed Project 5 w/ code
Write a Comment
User Comments (0)
About PowerShow.com