Transcript Slide 1
Lesson 28:
More on the GUI button, frame and actions
The Java Button
// helloworldbutton.java
RECAP
import java.awt.*;
import javax.swing.*;
class HelloButton{
public static void main (String[] args){
JFrame frame = new JFrame("HelloButton");
Container pane = frame.getContentPane();
JButton hello = new JButton("HelloWorld");
pane.add(hello);
// for wrapping the frame we could use this command
// frame.pack();
frame.setSize(300,200);
frame.show();
}
}
The new result
The source file
Compiling and running the program
The output
RECAP
More on the Swing library
• Any program that uses Swing to create a window enters a separate
thread of execution that enters an infinite loop, looking for events
such as mouse movements, button clicks or key presses.
• This is like having to workers executing the program:
– One for the main()
– One for watching for events
• This results in an event driven program
• When main() is finished, the program still is executing the “listening”
part of the library object.
As a result the program in Lesson 27 does not stop executing but
“hangs”. And we have to exit using the program commands in Windows
More on the Swing library
• All Swing components are event sources that can be observed or
listened to.
• To use the event, we need to tell the source which object to notify when
an event occurs.
• By default the JButton reacts to a mouse click by changing its
appearance.
• JButtons are also the source of java.awt.event.ActionEvent objects. An
ActionEvent is generated by a button when you click the button with a
mouse.
• Objects that can receive events are called listeners.
• Different type of events have different listeners.
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
An Example
Importing the abstract windows
toolkit and the Swing library
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
hello.addActionListener(listener);
Creating a class called
pane.add(hello);
HelloGoodBye this has to
be stored in a file named
frame.setSize(200,75);
HelloGoodBye.java (case
frame.show();
sensitive).
}
}
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
An Example
The main section. This means
we can execute this program
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
hello.addActionListener(listener);
Creating a java frame named frame
pane.add(hello);
with the label HelloGoodBye
frame.setSize(200,75);
frame.show();
}
}
The keyword (new) to identify a constructor
statement where objects are created
An Example
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
A container is a component to which we can add
other components. A JFrame is actually a container,
but we should not add components directly to a
JFrame. Instead, we should add all components to a
special container in the JFrame.
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
hello.addActionListener(listener);
pane.add(hello);
frame.setSize(200,75);
frame.show();
We use the method getContentPane()
}
to get a reference to JFrame’s special
}
container.
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
An Example
Here we create a Javax.swing.JButton object
and give it a string which is used as a label.
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
hello.addActionListener(listener);
pane.add(hello);
frame.setSize(200,75);
frame.show();
The object is named hello
}
and is a Java button (Jbutton)
}
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
An Example
Creating an object named listener based on the
class GoodBye (which we have not created yet).
This class will contain the events we want to execute
when the button is clicked.
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
hello.addActionListener(listener);
pane.add(hello);
frame.setSize(200,75);
frame.show();
Linking the listener object (we could have called this
}
something more descriptive), to our button through
}
the method addActionListener which was inherited
from JButton to our button called hello.
An Example
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
The method add in the class Container is
used to add a component to the JFrame’s
content pane (later we will add many
components to a frame).
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
hello.addActionListener(listener);
pane.add(hello);
frame.setSize(200,75);
frame.show();
Note: For now, we do not tell the container
}
how to arrange this component, nor where
}
this component is to be displayed (we will do
this later).
An Example
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
We can set the size of the frame by
using the method setSize from JFrame.
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
This method accepts 2
hello.addActionListener(listener);
parameters (width, height), which
pane.add(hello);
passed in terms of number of
frame.setSize(200,75);
pixels.
frame.show();
}
}
The show method from JFrame is
used to display the frame.
An Example
// HelloGoodBye.java
import java.awt.*;
import javax.swing.*;
Now we have a button, but no action for the
object named listener from the GoodBye
class
class HelloGoodBye{
public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");
Container pane = frame.getContentPane();
JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye();
hello.addActionListener(listener);
pane.add(hello);
frame.setSize(200,75);
frame.show();
}
}
Creating the GoodBye class
// GoodBye.java
import java.awt.event.*;
Importing the awt.event package from the library that
contains the various event listing interfaces.
class GoodBye implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println(“Goodbye !”);
System.exit(0);
}
}
This example use the interface ActionListener to indicate
that GoodBye contains the method actionPerformed().
An Example
// GoodBye.java
import java.awt.event.*;
Here we indicate that the class GoodBye contains all
methods specified in the interface ActionListener. That is to
say that the class implements the interface ActionListener
class GoodBye implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println(“Goodbye !”);
System.exit(0);
}
}
An Example
// GoodBye.java
import java.awt.event.*;
This method is called when the button is clicked. It is
executed through the actionPerformed method, which ,
as we covered before, for a JButton is a mouseclick.
class GoodBye implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println(“Goodbye !”);
System.exit(0);
}
}
In this example we do not
use the ActionEvent
parameter passed to the
method. We will use it in
later examples.
Prints Goodbye on the screen
(not in the frame)
Exits the program
The Result
The Files in Notepad
The Files in Command Prompt
Compiling and class files
The Result
The steps
• Create a button with new Jbutton (“some label”)
• Get the Container for the Jframe using getContentPane()
• Add the button to the content pane of the Jframe with add()
• Create an ActionEventListener class by
– Adding implements ActionEventListener to the lcass declaration and
– Defining an actionPerformed method
• Add the listener object to the list of listeners for the button by
calling button.addActionListener(listener), where the button
is the name we gave our button, and listener is an instance
of the class we created to be executed when the button was
clicked (the GoodBye class).