TextArea, text with global attributes
Example N experiments with a TextArea. Font attributes and colors can be changed for the text as a whole.
A TextArea
displays a multi-line text in a single font and color.
The attributes are set by buttons using a common ActionListener. Because font attributes can only be changed by introducing a new Font object
, they are recorded locally:
{apps/gui/N.java}
import awt.Button;
import awt.Panel;
import awt.TextArea;
import java.awt.*;
import java.awt.event.*;
/** A class to test text attributes in a text area */
public class N extends Panel {
protected TextArea t;
protected String font; protected int style; protected int size; public N () {
super(new BorderLayout());
add("Center", t = new TextArea(10, 40));
t.setFont(new Font(font = "SansSerif", style = Font.PLAIN, size = 12));
final ActionListener opt = new ActionListener() {
public void actionPerformed (ActionEvent e) {
t.requestFocus();
String cmd = e.getActionCommand();
if (cmd.equals("quit"))
System.exit(0);
else if (cmd.equals("serif"))
t.setFont(new Font(font = "Serif", style, size));
else if (cmd.equals("sans"))
t.setFont(new Font(font = "SansSerif", style, size));
else if (cmd.equals("mono"))
t.setFont(new Font(font = "Monospaced", style, size));
else if (cmd.equals("plain"))
t.setFont(new Font(font, style = Font.PLAIN, size));
else if (cmd.equals("bold"))
t.setFont(new Font(font, style = Font.BOLD, size));
else if (cmd.equals("italic"))
t.setFont(new Font(font, style = Font.ITALIC, size));
else if (cmd.equals("boldItalic"))
t.setFont(new Font(font, style = Font.BOLD+Font.ITALIC, size));
else if (cmd.equals("larger"))
t.setFont(new Font(font, style, size+=2));
else if (cmd.equals("smaller") && size > 2)
t.setFont(new Font(font, style, size-=2));
else if (cmd.equals("black"))
t.setForeground(Color.black);
else if (cmd.equals("red"))
t.setForeground(Color.red);
else if (cmd.equals("blue"))
t.setForeground(Color.blue);
else if (cmd.equals("green"))
t.setForeground(Color.green);
}
};