|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.awt.Component
java.awt.Container
acm.io.IOConsole
public class IOConsole
The 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.
|
|
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class java.awt.Container |
|---|
Container.AccessibleAWTContainer |
| Nested classes/interfaces inherited from class java.awt.Component |
|---|
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
| Field Summary | |
|---|---|
protected static Font |
DEFAULT_FONT
The default font used by a new IOConsole. |
protected static 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. |
| Fields inherited from class java.awt.Component |
|---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| Fields inherited from interface java.awt.image.ImageObserver |
|---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| Constructor Summary | |
|---|---|
IOConsole()
Creates a new IOConsole object. |
|
| Method Summary | |
|---|---|
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. |
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. |
Color |
getInputColor()
Returns the color currently in use for console input. |
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. |
Dimension |
getMinimumSize()
Overrides the getMinimumSize method to ensure that an
IOConsole is not too large. |
Dimension |
getPreferredSize()
Overrides the getPreferredSize method to ensure that an
IOConsole is not too large. |
BufferedReader |
getReader()
Returns a BufferedReader object that can be used to read
from the console. |
PrintWriter |
getWriter()
Returns a PrintWriter object that can be used to send
output to the console. |
boolean |
isConsoleMenuItem(JMenuItem item)
Returns true if the item is one that the console recognizes. |
boolean |
menuAction(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(Object x)
Makes sure that print can display an Object. |
void |
print(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(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(Object x)
Makes sure that println can display an Object. |
void |
println(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(String prompt)
Prompts the user to enter a boolean value, which is then returned as the value of this method. |
boolean |
readBoolean(String prompt,
String trueLabel,
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(String prompt)
Prompts the user to enter an double-precision number, which is then returned as the value of this method. |
double |
readDouble(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(String prompt)
Prompts the user to enter an integer, which is then returned as the value of this method. |
int |
readInt(String prompt,
int low,
int high)
Prompts the user to enter an integer, which is then returned as the value of this method. |
String |
readLine()
Reads and returns a line of input from the console, without including the end-of-line characters that terminate the input. |
String |
readLine(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(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(Color color)
Sets the background color used for the console. |
void |
setErrorColor(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(Font font)
Sets the font for the console. |
void |
setFont(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(Color color)
Sets the foreground color used for the output text. |
void |
setInputColor(Color color)
Sets the color used for console input. |
void |
setInputScript(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(String msg)
Displays the error message on the console. |
void |
updateMenuBar(ProgramMenuBar mbar)
Updates the menu bar to enable the appropriate menu items. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected static final Font DEFAULT_FONT
IOConsole.
protected static final String LINE_SEPARATOR
protected static final int MINIMUM_CONSOLE_HEIGHT
protected static final int MINIMUM_CONSOLE_WIDTH
public static final IOConsole SYSTEM_CONSOLE
IOConsole class, but which does so using the standard
I/O streams System.in and System.out.
| Constructor Detail |
|---|
public IOConsole()
IOConsole object.
| Method Detail |
|---|
public void clear()
public void copy()
protected acm.io.ConsoleModel createConsoleModel()
public void cut()
public Color getErrorColor()
public int getErrorStyle()
public boolean getExceptionOnError()
false to retry
on errors; true to raise an exception)public Color getInputColor()
public BufferedReader getInputScript()
null.
public int getInputStyle()
public ProgramMenuBar getMenuBar()
public Dimension getMinimumSize()
getMinimumSize method to ensure that an
IOConsole is not too large.
getMinimumSize in class Containerpublic Dimension getPreferredSize()
getPreferredSize method to ensure that an
IOConsole is not too large.
getPreferredSize in class Containerpublic BufferedReader getReader()
BufferedReader object that can be used to read
from the console.
BufferedReader that reads from this consolepublic PrintWriter getWriter()
PrintWriter object that can be used to send
output to the console.
PrintWriter that writes to this consolepublic boolean isConsoleMenuItem(JMenuItem item)
true if the item is one that the console recognizes.
public boolean menuAction(ActionEvent e)
public void paste()
public final void print(boolean x)
print can display a boolean.
print in interface IOModelpublic final void print(char x)
print can display a char.
print in interface IOModelpublic final void print(double x)
print can display a double.
print in interface IOModelpublic final void print(float x)
print can display a float.
print in interface IOModelpublic final void print(int x)
print can display an int.
print in interface IOModelpublic final void print(long x)
print can display a long.
print in interface IOModelpublic final void print(Object x)
print can display an Object.
print in interface IOModelpublic void print(String value)
print method is overloaded so that
value can be of any type.
print in interface IOModelvalue - The value to be displayedpublic void printConsole()
public void printConsole(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 println()
println in interface IOModelpublic final void println(boolean x)
println can display a boolean.
println in interface IOModelpublic final void println(char x)
println can display a char.
println in interface IOModelpublic final void println(double x)
println can display a double.
println in interface IOModelpublic final void println(float x)
println can display a float.
println in interface IOModelpublic final void println(int x)
println can display an int.
println in interface IOModelpublic final void println(long x)
println can display a long.
println in interface IOModelpublic final void println(Object x)
println can display an Object.
println in interface IOModelpublic void println(String value)
println method is
overloaded so that value can be of any type.
println in interface IOModelvalue - The value to be displayedpublic 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 IOModelpublic final boolean readBoolean(String prompt)
setExceptionOnError method.
readBoolean in interface IOModelprompt - The prompt string to display to the user
public boolean readBoolean(String prompt,
String trueLabel,
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 IOModelprompt - The prompt string to display to the usertrueLabel - The string used to indicate truefalseLabel - The string used to indicate false
public final double readDouble()
setExceptionOnError method.
readDouble in interface IOModeldouble
public final double readDouble(double low,
double high)
setExceptionOnError method.
readDouble in interface IOModellow - The lowest value in the permitted rangehigh - The highest value in the permitted range
doublepublic final double readDouble(String prompt)
setExceptionOnError method.
readDouble in interface IOModelprompt - The prompt string to display to the user
double
public double readDouble(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 IOModelprompt - The prompt string to display to the userlow - The lowest value in the permitted rangehigh - The highest value in the permitted range
doublepublic final int readInt()
setExceptionOnError method.
readInt in interface IOModel
public final int readInt(int low,
int high)
setExceptionOnError method.
readInt in interface IOModellow - The lowest value in the permitted rangehigh - The highest value in the permitted range
public final int readInt(String prompt)
setExceptionOnError method.
readInt in interface IOModelprompt - The prompt string to display to the user
public int readInt(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.
readInt in interface IOModelprompt - The prompt string to display to the userlow - The lowest value in the permitted rangehigh - The highest value in the permitted range
public final String readLine()
readLine in interface IOModelStringpublic String readLine(String prompt)
readLine in interface IOModelprompt - The prompt string to display to the user
Stringpublic void requestFocus()
requestFocus method so that it forwards to the
console model.
requestFocus in class Componentpublic void save()
public void save(Writer wr)
wr - A Writer to which the console output is sentpublic void saveAs()
public void script()
public void selectAll()
public void setBackground(Color color)
setBackground in class Componentcolor - The new background colorpublic void setErrorColor(Color color)
color - The color used for console error messagespublic 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 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 void setFont(Font font)
setFont in class Containerfont - The font to use for the consolepublic void setFont(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 setForeground(Color color)
setForeground in class Componentcolor - The color to use for the output textpublic void setInputColor(Color color)
color - The color used for console inputpublic void setInputScript(BufferedReader rd)
rd - The reader from which console input is takenpublic 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 void setMenuBar(ProgramMenuBar mbar)
mbar - The menu barpublic void showErrorMessage(String msg)
showErrorMessage in interface IOModelmsg - The error msg to be displayedpublic void updateMenuBar(ProgramMenuBar mbar)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||