Page 689 - Introduction to Programming with Java: A Problem Solving Approach
P. 689

                16.8 GreetingProgram 655
 /*************************************************************
*
Greeting.java
Dean & Dean
This program demonstrates text boxes and labels.
When the user presses Enter after typing something into the
text box, the text box value displays in the label below.
*************************************************************/
*
*
*
*
*
import javax.swing.*;
// for JFrame, JLabel, JTextField
// for FlowLayout
import java.awt.event.*; // for ActionListener, ActionEvent
import java.awt.*;
   public class Greeting extends JFrame
{
private static final int WIDTH = 325;
private static final int HEIGHT = 100;
private JTextField nameBox; // holds user's name
private JLabel greeting;
// personalized greeting
//**********************************************************
       Apago PDF Enhancer
public Greeting()
{
setTitle("Greeting");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
// end constructor
//**********************************************************
}
// Create components and add them to window.
private void createContents()
{
}
JLabel namePrompt = new JLabel("What's your name?");
nameBox = new JTextField(15);
greeting = new JLabel();
add(namePrompt);
add(nameBox);
add(greeting);
nameBox.addActionListener(new Listener());
// end createContents
3. Register a listener.
4. Import this package for event handling.
   Figure 16.5a Greeting program—part A













































   687   688   689   690   691