Java Reference
In-Depth Information
theColor = c;
thePanel = p;
}
public void
actionPerformed(ActionEvent e)
{
thePanel.setBackground(theColor);
}
}
//End of ColorListener inner class
Replace
redChoice.addActionListener(
new
RedListener());
with
redChoice.addActionListener(
new
ColorListener(Color.RED, redPanel));
Also make similar changes to the menu items
whiteChoice
and
blueChoice
, with
the obvious changes to colors and panels.
This is not really preferable to what we did in Display 17.16, but it is a good
exercise. The complete program done this way is on the accompanying CD in
the file named
InnerListenersDemo2.java
.
extra code
on CD
36.
A
JTextField
object displays only a single line. A
JTextArea
object can display
more than one line of text.
37.
The contents of the text field would change to
"Hello Hello Hello "
followed
by your name.
extra code
on CD
38.
This program is on the CD that accompanies this text.
import
javax.swing.JFrame;
import
javax.swing.JTextArea;
import
javax.swing.JPanel;
import
javax.swing.JLabel;
import
javax.swing.JButton;
import
java.awt.GridLayout;
import
java.awt.BorderLayout;
import
java.awt.FlowLayout;
import
java.awt.Color;
import
java.awt.event.ActionListener;
import
java.awt.event.ActionEvent;
public class
TextAreaDemo
extends
JFrame
implements
ActionListener
{
public static final int
WIDTH = 400;
public static final int
HEIGHT = 200;
public static final int
NUMBER_OF_LINES = 10;