Solutions to the Try It Out Exercises in Java For Dummies, 7th Edition
by Barry Burd

Chapter 16: Responding to Keystrokes and Mouse Clicks

In this chapter:

Remove the actionPerformed method

You get an error message indicating that the actionPerformed method is missing. You get this message because your GameFrame class implements ActionListener.

Remove the call to setVisible(true)

Nothing happens when you run the program. (The frame doesn't become visible.)

Remove the call to pack()

The frame appears, but it's too small to contain the text field, the button and the label. The only way for you to see these components is to enlarge the frame by dragging the edge of the frame with your mouse.

Remove the call to button.addActionListener()

The actionPerformed method is no longer called when the user clicks the button. So when you click the button, nothing happens.

Describe the uses of the keyword this in the sample code

The first use of the keyword this forces the expression this.n to refer to the field named n, as in
private int n;
instead of referring to the parameter named n, as in
IntegerHolder(int n)
The second use of the keyword this refers to an instance of the IntegerHolder class (whichever instance is executing its own displayMyN method at the time). This instance gets passed to the Displayer class's display method. In the display method's body, that instance goes by the parameter name holder. The display method calls that holder's getN method. In other words, the display method calls the IntegerHolder instance's getN method.

When the user clicks a button, copy text from a text field to a label

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame implements ActionListener {
   private static final long serialVersionUID = 1L;

   JTextField textField = new JTextField(5);
   JButton button = new JButton("Copy");
   JLabel label = new JLabel("(the label)");

   public Copier() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new FlowLayout());
      add(textField);
      add(button);
      add(label);
      button.addActionListener(this);
      pack();
      setVisible(true);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      String textFieldText = textField.getText();
      label.setText(textFieldText);
   }
}


public class ShowCopierFrame {

   public static void main(String args[]) {
       new Copier();
   }
}

When the user types in a text field, copy text from the text field to a label

import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame implements KeyListener {
  private static final long serialVersionUID = 1L;

  JTextField textField = new JTextField(5);
  JLabel label = new JLabel("(the label)");

  public Copier() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    add(textField);
    textField.addKeyListener(this);
    add(label);
    pack();
    setVisible(true);
  }

  @Override
  public void keyTyped(KeyEvent e) {
  }

  @Override
  public void keyPressed(KeyEvent e) {
  }

  @Override
  public void keyReleased(KeyEvent e) {
    label.setText(textField.getText());
  }

}

Create an inner class

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame {
  private static final long serialVersionUID = 1L;

  JTextField textField = new JTextField(5);
  JButton button = new JButton("Copy");
  JLabel label = new JLabel("(the label)");

  public Copier() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    add(textField);
    add(button);
    add(label);
    button.addActionListener(new MyActionListener());
    pack();
    setVisible(true);
  }

  class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
      String textFieldText = textField.getText();
      label.setText(textFieldText);
    }
  }
}

Create an annonymous inner class

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Copier extends JFrame {
  private static final long serialVersionUID = 1L;

  JTextField textField = new JTextField(5);
  JButton button = new JButton("Copy");
  JLabel label = new JLabel("(the label)");

  public Copier() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    add(textField);
    add(button);
    add(label);

    button.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        String textFieldText = textField.getText();
        label.setText(textFieldText);
      }
    });

    pack();
    setVisible(true);
  }
}