T u t o

advertisement
Tutorial 5
Topics
Interfaces
o What is an interface
o ActionListener
Introduction to Java Swing
o What is Swing
o SwingApplication
o Step-by-Step guide
Design Problems
Interface
What is an Interface
An interface is a collection of method declarations which can be implemented
by classes
An interface describes what classes should do, without specifying how they
should do it
The how will be defined in the classes that implement the interface
Each class defines the implementation differently
A class can implement one or more interfaces
An interface can contain both methods and constants
To use an interface, a class must
Implement that interface
Define ALL methods in that interface
ActionListener
ActionListener is a Java interface implemented by many GUI components, such as
buttons. It has only one method – actionPerformed. Here is its definition:
public Interface ActionListener {
public void actionPerformed(ActionEvent e)
}
The following example is a Java GUI application called SwingApplication which has a
“button-click” counter – every time user clicks the button, the counter increases by 1.
We will explain how to create the GUI in the next section. Here, we just want to
demonstrate how one should implement ActionListener.
class SwingApplication implements ActionListener
{
………………….
……………………
int numClicks = 0; //”click-counter”
JLabel label = new JLabel(“Number of button clicks: ” + numClicks);
JButton button = new JButton("I'm a Swing button!");
button.addActionListener(this); //Add an actionListener to the button
……………………
public void actionPerformed(ActionEvent e)
{
numClicks++;
label.setText(“Number of button clicks: “ + numClicks);
}
}
Here, the class SwingApplication implements the ActionListener interface. Inside the
class definition, it re-defined the actionPerformed method: every time user clicks the
button, the counter in the message increases by 1.
Introducing the Java Swing
What is Swing
The Swing package is part of the JavaTM Foundation Classes (JFC) in the Java
platform. The JFC encompasses a group of features to help people build GUIs. Here
are some commonly-used Swing components:
List
Buttons
Combo box
Scroll pane
Menu
Dialog
Table
Frame
SwingApplication
The SwingApplication is an example we pulled from the Java Tutorial. As mentioned
earlier, it is a “button-click” counter - every time the user clicks the button, the label
is updated showing the counter increasing by 1.
SwingApplication has four Swing components:
A frame (JFrame). The frame is a top-level container. It provides a place for
other Swing components to paint themselves. The other commonly used toplevel containers are dialogs (JDialog) and applets (JApplet).
A panel (JPanel). The panel is an intermediate container. Its only purpose is to
simplify the positioning of the button and label. Other intermediate Swing
containers include JScrollPane (scroll panes) and JTabbedPane (tabbed panes)
A button (JButton) and a label (JLabel). The button and label are atomic
components -- components that exist not to hold other Swing components, but
to interface with the user. The Swing API provides many atomic components,
including combo boxes (JComboBox), text fields (JTextField), and tables
(JTable).
Here is a diagram of the containment hierarchy for the window shown by
SwingApplication
Here is the code that adds the button and label to the panel, and the panel to the
content pane:
frame = new JFrame(...);
pane = new JPanel();
button = new JButton(...);
label = new JLabel(...);
pane.add(button);
pane.add(label);
frame.getContentPane().add(pane, BorderLayout.CENTER);
Step-by-step guide
Here is a step-by-step guide of how to create this SwingApplication:
Setting up the top-level container
Setting up buttons and labels
Adding components to containers
Handling events
Next we will discuss each step in length.
Setting up the top-level container
//Create the top-level container titled “SwingApplicatoin”
JFrame frame = new JFrame("SwingApplication");
..................
frame.pack();
frame.setVisible(true);
Setting up buttons and labels
//Create a button
JButton button = new JButton("I'm a Swing button!");
//Create a label
JLabel label = new JLabel(“Number of button clicks: “ + "0
//Set the label text
int numClicks = 0;
label.setText(“Number of button clicks: “ + numClicks);
");
Adding components to containers
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(0, 1));
pane.add(button);
pane.add(label);
frame.getContentPane().add(pane, BorderLayout.CENTER);
Handling events
We will discuss this feature in the next tutorial.
Design Problem
Interface
You are hired to develop an accounting program that calculates the weekly salary of
three types of employees: management, union workers, and contractors.
Each management employee has a social security number (SSN), name, title, and
salary. There is no overtime pay for management employees
Each union worker has a SSN, name, hourly rate, and hours worked. Union rules
require that we pay union workers overtime, which is 1.5 times the hourly rate for the
hours they put in beyond 40
Each contractor has a SSN, name, agency, hours worked, and hourly rate. We don’t
have to pay contractors at a higher rate for overtime. However, there is a cap on how
much each contractor can make each week. Currently, the cap is 60 * hourly rate.
Please define a set of classes that model this situation. Here are some requirements:
Create an abstract class called Employee which has a method called print
Create an interface Compensation which has one method called calculatePay
Create three subclasses: Management, UnionWorker, Contractor which inherit from
Employee
Each subclass will implement the Compensation interface
Each subclass must print all its information. For example, here is the printout for a
manager:
SSN: 1111
Name: Wen Xiao
Salary: $9500
Basic Swing component
Download the code of SwingApplication.java and add a menu bar to the frame. The
menu bar has two menus: File and Edit. Within File, there are 3 menu items: Open,
Save, and Exit. Within Edit, there are 2 menu items: Copy and Paste. You don’t need
to implement any event-handling functions, just the GUI. (There is a short course on
how to use menus at
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html. Make sure
the program compiles and runs.
Download