public class IOConsole extends java.awt.Container implements IOModel
IOConsole
class makes it easier to interact with the user
using text-based input and output in the style of a traditional console.
Given a IOConsole
object, you can write output to that console
using the print
and
println
methods, just as you
would for the standard output stream. To request input from the user, the
most common methods are
A IOConsole
object is a lightweight component and must be added
to an installed Frame
or JFrame
before it becomes
visible on the screen. The usual strategy for including a console in a frame
is to use the
ConsoleProgram
mechanism in the acm.program
package.
The operation of the IOConsole
class is illustrated by the
following test
method, which generates the session shown on the
right. The user input appears in blue, just as it
does in the console window.
|
Modifier and Type | Field and Description |
---|---|
protected static java.awt.Font |
DEFAULT_FONT
The default font used by a new
IOConsole . |
protected static java.lang.String |
LINE_SEPARATOR
The end-of-line separator for this platform.
|
protected static int |
MINIMUM_CONSOLE_HEIGHT
The minimum height for a console.
|
protected static int |
MINIMUM_CONSOLE_WIDTH
The minimum width for a console.
|
static IOConsole |
SYSTEM_CONSOLE
This constant is an object that offers the functionality of the
IOConsole class, but which does so using the standard I/O
streams System.in and System.out . |
Constructor and Description |
---|
IOConsole()
Creates a new
IOConsole object. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the console display.
|
void |
copy()
Implements the "Copy" menu operation.
|
protected acm.io.ConsoleModel |
createConsoleModel()
Creates the console model used by this console.
|
void |
cut()
Implements the "Cut" menu operation.
|
java.awt.Color |
getErrorColor()
Returns the color currently in use for console error messages.
|
int |
getErrorStyle()
Returns the current style parameters for console error messages.
|
boolean |
getExceptionOnError()
Returns the state of the error-handling flag.
|
java.awt.Color |
getInputColor()
Returns the color currently in use for console input.
|
java.io.BufferedReader |
getInputScript()
Retrieves the input script.
|
int |
getInputStyle()
Returns the current style parameters for console input.
|
ProgramMenuBar |
getMenuBar()
Returns the menu bar that controls this console.
|
java.awt.Dimension |
getMinimumSize()
Overrides the
getMinimumSize method to ensure that an
IOConsole is not too large. |
java.awt.Dimension |
getPreferredSize()
Overrides the
getPreferredSize method to ensure that an
IOConsole is not too large. |
java.io.BufferedReader |
getReader()
Returns a
BufferedReader object that can be used to read from
the console. |
java.io.PrintWriter |
getWriter()
Returns a
PrintWriter object that can be used to send output to
the console. |
boolean |
isConsoleMenuItem(javax.swing.JMenuItem item)
Returns
true if the item is one that the console recognizes. |
boolean |
menuAction(java.awt.event.ActionEvent e)
Called whenever a relevant action is detected in the menu bar.
|
void |
paste()
Implements the "Paste" menu operation.
|
void |
print(boolean x)
Makes sure that
print can display a boolean . |
void |
print(char x)
Makes sure that
print can display a char . |
void |
print(double x)
Makes sure that
print can display a double . |
void |
print(float x)
Makes sure that
print can display a float . |
void |
print(int x)
Makes sure that
print can display an int . |
void |
print(long x)
Makes sure that
print can display a long . |
void |
print(java.lang.Object x)
Makes sure that
print can display an Object . |
void |
print(java.lang.String value)
Displays the argument value on the console, leaving the cursor at the end of
the output.
|
void |
printConsole()
Implements the "Print Console" menu operation.
|
void |
printConsole(java.awt.PrintJob pj)
Prints the console output using the specified
PrintJob object. |
void |
println()
Advances the console cursor to the beginning of the next line.
|
void |
println(boolean x)
Makes sure that
println can display a boolean . |
void |
println(char x)
Makes sure that
println can display a char . |
void |
println(double x)
Makes sure that
println can display a double . |
void |
println(float x)
Makes sure that
println can display a float . |
void |
println(int x)
Makes sure that
println can display an int . |
void |
println(long x)
Makes sure that
println can display a long . |
void |
println(java.lang.Object x)
Makes sure that
println can display an Object . |
void |
println(java.lang.String value)
Displays the argument value on the console and then advances the cursor to
the beginning of the next line.
|
boolean |
readBoolean()
Reads and returns a boolean value from the user, which must match either
true or false , ignoring case. |
boolean |
readBoolean(java.lang.String prompt)
Prompts the user to enter a boolean value, which is then returned as the
value of this method.
|
boolean |
readBoolean(java.lang.String prompt,
java.lang.String trueLabel,
java.lang.String falseLabel)
Prompts the user to enter a value, which is interpreted as a boolean value by
matching it against the
trueLabel and falseLabel
parameters. |
double |
readDouble()
Reads and returns a double-precision value from the user.
|
double |
readDouble(double low,
double high)
Reads and returns a double-precision value from the user, which is
constrained to be within the specified inclusive range.
|
double |
readDouble(java.lang.String prompt)
Prompts the user to enter an double-precision number, which is then returned
as the value of this method.
|
double |
readDouble(java.lang.String prompt,
double low,
double high)
Prompts the user to enter an double-precision number, which is then returned
as the value of this method.
|
int |
readInt()
Reads and returns an integer value from the user.
|
int |
readInt(int low,
int high)
Reads and returns an integer value from the user, which is constrained to be
within the specified inclusive range.
|
int |
readInt(java.lang.String prompt)
Prompts the user to enter an integer, which is then returned as the value of
this method.
|
int |
readInt(java.lang.String prompt,
int low,
int high)
Prompts the user to enter an integer, which is then returned as the value of
this method.
|
java.lang.String |
readLine()
Reads and returns a line of input from the console, without including the
end-of-line characters that terminate the input.
|
java.lang.String |
readLine(java.lang.String prompt)
Prompts the user to enter a line of text, which is then returned as the value
of this method.
|
void |
requestFocus()
Overrides the
requestFocus method so that it forwards to the
console model. |
void |
save()
Implements the "Save" menu operation.
|
void |
save(java.io.Writer wr)
Copies the console output to the specified writer.
|
void |
saveAs()
Implements the "Save As" menu operation.
|
void |
script()
Implements the "Script" menu operation.
|
void |
selectAll()
Implements the "Select All" menu operation.
|
void |
setBackground(java.awt.Color color)
Sets the background color used for the console.
|
void |
setErrorColor(java.awt.Color color)
Sets the color used for console error messages.
|
void |
setErrorStyle(int style)
Sets the style parameters for console error messages.
|
void |
setExceptionOnError(boolean flag)
Sets the error-handling mode of the console as specified by the
flag parameter. |
void |
setFont(java.awt.Font font)
Sets the font for the console.
|
void |
setFont(java.lang.String str)
Sets the font used for the console as specified by the string
str , which is interpreted in the style of
Font.decode . |
void |
setForeground(java.awt.Color color)
Sets the foreground color used for the output text.
|
void |
setInputColor(java.awt.Color color)
Sets the color used for console input.
|
void |
setInputScript(java.io.BufferedReader rd)
Sets a new input script for the console, which will subsequently take input
from the specified reader.
|
void |
setInputStyle(int style)
Sets the style parameters for console input.
|
void |
setMenuBar(ProgramMenuBar mbar)
Sets the menu bar that controls this console.
|
void |
showErrorMessage(java.lang.String msg)
Displays the error message on the console.
|
void |
updateMenuBar(ProgramMenuBar mbar)
Updates the menu bar to enable the appropriate menu items.
|
add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, isValidateRoot, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, update, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final IOConsole SYSTEM_CONSOLE
IOConsole
class, but which does so using the standard I/O
streams System.in
and System.out
.protected static final java.awt.Font DEFAULT_FONT
IOConsole
.protected static final java.lang.String LINE_SEPARATOR
protected static final int MINIMUM_CONSOLE_WIDTH
protected static final int MINIMUM_CONSOLE_HEIGHT
public void clear()
public void print(java.lang.String value)
print
method is overloaded so that
value
can be of any type.public final void print(boolean x)
print
can display a boolean
.public final void print(char x)
print
can display a char
.public final void print(double x)
print
can display a double
.public final void print(float x)
print
can display a float
.public final void print(int x)
print
can display an int
.public final void print(long x)
print
can display a long
.public final void print(java.lang.Object x)
print
can display an Object
.public void println()
public void println(java.lang.String value)
println
method is overloaded
so that value
can be of any type.public final void println(boolean x)
println
can display a boolean
.public final void println(char x)
println
can display a char
.public final void println(double x)
println
can display a double
.public final void println(float x)
println
can display a float
.public final void println(int x)
println
can display an int
.public final void println(long x)
println
can display a long
.public final void println(java.lang.Object x)
println
can display an Object
.public void showErrorMessage(java.lang.String msg)
showErrorMessage
in interface IOModel
msg
- The error msg to be displayedpublic final java.lang.String readLine()
public java.lang.String readLine(java.lang.String prompt)
public final int readInt()
setExceptionOnError
method.public final int readInt(int low, int high)
setExceptionOnError
method.public final int readInt(java.lang.String prompt)
setExceptionOnError
method.public int readInt(java.lang.String prompt, int low, int high)
low
and high
. If the user types a value that is not
a legal integer or is outside the specified range, the method ordinarily
offers the user a chance to reenter the data, although this behavior can be
changed using the
setExceptionOnError
method.public final double readDouble()
setExceptionOnError
method.readDouble
in interface IOModel
double
public final double readDouble(double low, double high)
setExceptionOnError
method.readDouble
in interface IOModel
low
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangedouble
public final double readDouble(java.lang.String prompt)
setExceptionOnError
method.readDouble
in interface IOModel
prompt
- The prompt string to display to the userdouble
public double readDouble(java.lang.String prompt, double low, double high)
low
and high
. If the user types a value
that is not a legal number, the method ordinarily offers the user a chance to
reenter the data, although this behavior can be changed using the
setExceptionOnError
method.readDouble
in interface IOModel
prompt
- The prompt string to display to the userlow
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangedouble
public final boolean readBoolean()
true
or false
, ignoring case. If the user types a
value that is not one of these possibilities, the method ordinarily offers
the user a chance to reenter the data, although this behavior can be changed
using the
setExceptionOnError
method.readBoolean
in interface IOModel
public final boolean readBoolean(java.lang.String prompt)
setExceptionOnError
method.readBoolean
in interface IOModel
prompt
- The prompt string to display to the userpublic boolean readBoolean(java.lang.String prompt, java.lang.String trueLabel, java.lang.String falseLabel)
trueLabel
and falseLabel
parameters. If the user types a value that is not one of the two choices,
readBoolean
ordinarily offers the user a chance to reenter the
data, although this behavior can be changed using the
setExceptionOnError
method.readBoolean
in interface IOModel
prompt
- The prompt string to display to the usertrueLabel
- The string used to indicate true
falseLabel
- The string used to indicate false
public java.io.BufferedReader getReader()
BufferedReader
object that can be used to read from
the console.BufferedReader
that reads from this consolepublic java.io.PrintWriter getWriter()
PrintWriter
object that can be used to send output to
the console.PrintWriter
that writes to this consolepublic void setExceptionOnError(boolean flag)
flag
parameter. If flag
is false
(which is the default), the input methods give the user a chance to retry
after erroneous input. If this value is set to true
, illegal
input raises an
ErrorException
instead.flag
- false
to retry on errors; true
to raise
an exceptionpublic boolean getExceptionOnError()
false
to
retry on errors; true
to raise an exception)public void setInputStyle(int style)
Font.PLAIN
or a sum of one or more of the attributes
Font.BOLD
and Font.ITALIC
.style
- The style attributes to be used for console inputpublic int getInputStyle()
public void setInputColor(java.awt.Color color)
color
- The color used for console inputpublic java.awt.Color getInputColor()
public void setErrorStyle(int style)
Font.PLAIN
or a sum of one or more of the attributes
Font.BOLD
and Font.ITALIC
.style
- The style attributes to be used for console error messagespublic int getErrorStyle()
public void setErrorColor(java.awt.Color color)
color
- The color used for console error messagespublic java.awt.Color getErrorColor()
public void setFont(java.lang.String str)
str
, which is interpreted in the style of
Font.decode
. The usual format of the font string is
family-
style-
size
where both style and size are optional. If any of these parts are specified as an asterisk, the existing value is retained.
str
- A String
specifying the new fontpublic void setInputScript(java.io.BufferedReader rd)
rd
- The reader from which console input is takenpublic java.io.BufferedReader getInputScript()
null
.public void cut()
public void copy()
public void paste()
public void selectAll()
public void save()
public void saveAs()
public void save(java.io.Writer wr)
wr
- A Writer
to which the console output is sentpublic void printConsole()
public void printConsole(java.awt.PrintJob pj)
PrintJob
object.
This method is usually invoked from the Print
menu item in the
ProgramMenuBar
class and is not ordinarily called by clients.pj
- PrintJob
object to which the output is sentpublic void script()
public void setMenuBar(ProgramMenuBar mbar)
mbar
- The menu barpublic ProgramMenuBar getMenuBar()
public boolean menuAction(java.awt.event.ActionEvent e)
public boolean isConsoleMenuItem(javax.swing.JMenuItem item)
true
if the item is one that the console recognizes.public void updateMenuBar(ProgramMenuBar mbar)
public void setBackground(java.awt.Color color)
setBackground
in class java.awt.Component
color
- The new background colorpublic void setForeground(java.awt.Color color)
setForeground
in class java.awt.Component
color
- The color to use for the output textpublic void setFont(java.awt.Font font)
setFont
in class java.awt.Container
font
- The font to use for the consolepublic void requestFocus()
requestFocus
method so that it forwards to the
console model.requestFocus
in class java.awt.Component
public java.awt.Dimension getPreferredSize()
getPreferredSize
method to ensure that an
IOConsole
is not too large.getPreferredSize
in class java.awt.Container
public java.awt.Dimension getMinimumSize()
getMinimumSize
method to ensure that an
IOConsole
is not too large.getMinimumSize
in class java.awt.Container
protected acm.io.ConsoleModel createConsoleModel()