Java Reference
In-Depth Information
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.
38. This program is on the website that accompanies this text.
extra code
on website
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;
public static final int
NUMBER_OF_CHAR = 30;
private
JTextArea story;
public static void
main(String[] args)
{
TextAreaDemo gui =
new
TextAreaDemo();
gui.setVisible(
true
);
}
public
TextAreaDemo()
{
setTitle("Text Area Demo");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(
new
GridLayout(2, 1));
JPanel storyPanel =
new
JPanel();
storyPanel.setLayout(
new
BorderLayout());
storyPanel.setBackground(Color.WHITE);
story =
new
JTextArea(NUMBER_OF_LINES, NUMBER_OF_CHAR);