Java Reference
In-Depth Information
Our class implements a custom interface called
Outputable
.Itholds two
methods
print (String)
and
println (String)
:
public interface
Outputable
{
static final char CR ='
\
n';
// A method to print a string
public void print (String str);
// A method to print a string with a carriage return
public void println (String str);
}
These methods are intended to correspond to the print methods in
System.out
except that they display their strings on the graphics display rather than on the
console. The code below shows how we implement the
Outputable
methods
with the
JTextArea
component:
import java.awt.*;
import javax.swing.*;
/**
*This JPanel subclass holds a JTextArea object in a
*JScrollPane area. It also implements our Outputable
*interface to provide print (String) and println (String)
*methods similar to those in System.out.
**/
public class
TextOutputPanel
extends JPanel
implements Outputable
{
// A Swing textarea for display of string info
JTextArea fTextArea;
public TextOutputPanel ()
{
// A BorderLayout would be more appropriate here but
// it isn't discussed until chapter 7.
setLayout (new GridLayout (1,1));
// Create an instance of JTextArea
fTextArea = new JTextArea ();
fTextArea.setEditable (false);
// Add to a scroll pane so that a long list of
Search WWH ::
Custom Search