JTextFields

Globally:

JTextField input;

In init:

input = new JTextField (5);
The initial textfield is 5 characters long
add (input); Adds the textfield to the applet

Formatting (also in init):

input.setBackground(Color.yellow);
sets background colour of button
input.setForeground(Color.blue); sets colour of words
input.setFont(new Font("Arial", Font.BOLD, 18)); sets the font of the words

In Action Performed:

String name = input.getText (); get the text off the textfield
input.setText (""); clears the text on a textfield
input.setText("hello!") to set the text on the textfield
int x = Integer.parseInt(input.getText(); pulls out an integer, converting type along the way

try {
int x = Integer.parseInt (input.getText ());
}
catch (java.lang.NumberFormatException m)
{ showStatus ("Please enter an integer");
}

pulls out an integer, if the user messes up and enters a letter, it gives an error instead of crashing

A program:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class textfield extends Applet implements ActionListener
{
JTextField input;

public void init ()
{
input = new JTextField (20);
add (input);
}


public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("ok"))
{
String name = input.getText ();
input.setText ("");
}
}
}