object/container
Download
Report
Transcript object/container
CIS3931 - Intro to JAVA
Lecture Notes Set 10
28-June-05
GUI Programming – Buttons and
Actions.
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
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
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);
Button Example Code
• See ButtonDemo.java
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)
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 aren’t always the
best
• If you don’t specify one, a default layout
manager is chosen.
• ALWAYS choose the layout manager BEFORE
you add components to the frame.
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
FlowLayout Manager
Note : The layout of the frame will change if you add
components in a different order.
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();
. . . . .
}
}
Sample Program with Layout
Manager
• See LayoutDemo1.java
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);
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
ActionListener() example
• Changing the background color with a
button click
• See ActionDemo1.java
An addition to the example…
• Allowing the button to change colors
multiple times…
• See ActionDemo2.java
Demos
• Demo of completed Project 4 w/ code
• Demo of completed Project 5 w/ code