Java Software Solutions - Chapter 4: Writing Classes
We've been using predefined classes from the Java API. Now we will learn to write our own classes.
Chapter 4 focuses on:
class definitions
instance data
encapsulation and Java modifiers
method declaration and parameter passing
constructors
graphical objects
events and listeners
buttons and text fields
tton class It generates an action event The PushCounter example displays a push button that increments a counter each time it is pushed See PushCounter.java See PushCounterPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // PushCounter.java Authors: Lewis/Loftus // // Demonstrates a graphical user interface and an event listener. //******************************************************************** import javax.swing.JFrame; public class PushCounter { //----------------------------------------------------------------- // Creates the main program frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Push Counter"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new PushCounterPanel()); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // PushCounter.java Authors: Lewis/Loftus // // Demonstrates a graphical user interface and an event listener. //******************************************************************** import javax.swing.JFrame; public class PushCounter { //----------------------------------------------------------------- // Creates the main program frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Push Counter"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new PushCounterPanel()); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // PushCounterPanel.java Authors: Lewis/Loftus // // Demonstrates a graphical user interface and an event listener. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PushCounterPanel extends JPanel { private int count; private JButton push; private JLabel label; //----------------------------------------------------------------- // Constructor: Sets up the GUI. //----------------------------------------------------------------- public PushCounterPanel () { count = 0; push = new JButton ("Push Me!"); push.addActionListener (new ButtonListener()); continue Copyright © 2012 Pearson Education, Inc. continue label = new JLabel ("Pushes: " + count); add (push); add (label); setPreferredSize (new Dimension(300, 40)); setBackground (Color.cyan); } //***************************************************************** // Represents a listener for button push (action) events. //***************************************************************** private class ButtonListener implements ActionListener { //-------------------------------------------------------------- // Updates the counter and label when the button is pushed. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { count++; label.setText("Pushes: " + count); } } } Push Counter Example The components of the GUI are the button, a label to display the counter, a panel to organize the components, and the main frame The PushCounterPanel class represents the panel used to display the button and label The PushCounterPanel class is derived from JPanel using inheritance The constructor of PushCounterPanel sets up the elements of the GUI and initializes the counter to zero Copyright © 2012 Pearson Education, Inc. Push Counter Example The ButtonListener class is the listener for the action event generated by the button It is implemented as an inner class, which means it is defined within the body of another class That facilitates the communication between the listener and the GUI components Inner classes should only be used in situations where there is an intimate relationship between the two classes and the inner class is not needed in any other context Copyright © 2012 Pearson Education, Inc. Push Counter Example Listener classes are written by implementing a listener interface The ButtonListener class implements the ActionListener interface An interface is a list of methods that the implementing class must define The only method in the ActionListener interface is the actionPerformed method The Java API contains interfaces for many types of events We discuss interfaces in more detail in Chapter 6 Copyright © 2012 Pearson Education, Inc. Push Counter Example The PushCounterPanel constructor: instantiates the ButtonListener object establishes the relationship between the button and the listener by the call to addActionListener When the user presses the button, the button component creates an ActionEvent object and calls the actionPerformed method of the listener The actionPerformed method increments the counter and resets the text of the label Copyright © 2012 Pearson Education, Inc. Quick Check Copyright © 2012 Pearson Education, Inc. Which object in the Push Counter example generated the event? What did it do then? Quick Check Copyright © 2012 Pearson Education, Inc. Which object in the Push Counter example generated the event? What did it do then? The button component generated the event. It called the actionPerformed method of the listener object that had been registered with it. Text Fields Let's look at another GUI example that uses another type of component A text field allows the user to enter one line of input If the cursor is in the text field, the text field object generates an action event when the enter key is pressed See Fahrenheit.java See FahrenheitPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Fahrenheit.java Author: Lewis/Loftus // // Demonstrates the use of text fields. //******************************************************************** import javax.swing.JFrame; public class Fahrenheit { //----------------------------------------------------------------- // Creates and displays the temperature converter GUI. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Fahrenheit"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); FahrenheitPanel panel = new FahrenheitPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Fahrenheit.java Author: Lewis/Loftus // // Demonstrates the use of text fields. //******************************************************************** import javax.swing.JFrame; public class Fahrenheit { //----------------------------------------------------------------- // Creates and displays the temperature converter GUI. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Fahrenheit"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); FahrenheitPanel panel = new FahrenheitPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // FahrenheitPanel.java Author: Lewis/Loftus // // Demonstrates the use of text fields. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FahrenheitPanel extends JPanel { private JLabel inputLabel, outputLabel, resultLabel; private JTextField fahrenheit; //----------------------------------------------------------------- // Constructor: Sets up the main GUI components. //----------------------------------------------------------------- public FahrenheitPanel() { inputLabel = new JLabel ("Enter Fahrenheit temperature:"); outputLabel = new JLabel ("Temperature in Celsius: "); resultLabel = new JLabel ("---"); fahrenheit = new JTextField (5); fahrenheit.addActionListener (new TempListener()); continue Copyright © 2012 Pearson Education, Inc. continue add (inputLabel); add (fahrenheit); add (outputLabel); add (resultLabel); setPreferredSize (new Dimension(300, 75)); setBackground (Color.yellow); } //***************************************************************** // Represents an action listener for the temperature input field. //***************************************************************** private class TempListener implements ActionListener { //-------------------------------------------------------------- // Performs the conversion when the enter key is pressed in // the text field. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { int fahrenheitTemp, celsiusTemp; String text = fahrenheit.getText(); continue Copyright © 2012 Pearson Education, Inc. continue fahrenheitTemp = Integer.parseInt (text); celsiusTemp = (fahrenheitTemp-32) * 5/9; resultLabel.setText (Integer.toString (celsiusTemp)); } } } Fahrenheit Example Like the PushCounter example, the GUI is set up in a separate panel class The TempListener inner class defines the listener for the action event generated by the text field The FahrenheitPanel constructor instantiates the listener and adds it to the text field When the user types a temperature and presses enter, the text field generates the action event and calls the actionPerformed method of the listener Copyright © 2012 Pearson Education, Inc. Summary Chapter 4 focused on: class definitions instance data encapsulation and Java modifiers method declaration and parameter passing constructors graphical objects events and listeners buttons and text fields Copyright © 2012 Pearson Education, Inc.
File đính kèm:
- LESSON_04_Writing Classes.pptx