|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.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.
|
Field Summary | |
---|---|
IOConsole |
SYSTEM_CONSOLE
IOConsole class, but which does so using the standard
I/O streams System.in and System.out . |
Constructor Summary | |
---|---|
IOConsole()
IOConsole object. |
Method Summary | |
---|---|
void |
clear()
|
boolean |
getExceptionOnError()
|
BufferedReader |
getInputScript()
|
ProgramMenuBar |
getMenuBar()
|
BufferedReader |
getReader()
BufferedReader object that can be used to read
from the console. |
PrintWriter |
getWriter()
PrintWriter object that can be used to send
output to the console. |
boolean |
isConsoleMenuItem(JMenuItem item)
true if the item is one that the console recognizes. |
boolean |
menuAction(ActionEvent e)
|
void |
print(String value)
|
void |
println()
|
void |
println(String value)
|
boolean |
readBoolean()
true or false , ignoring case. |
boolean |
readBoolean(String prompt)
|
boolean |
readBoolean(String prompt,
String trueLabel,
String falseLabel)
trueLabel and falseLabel
parameters. |
double |
readDouble()
|
double |
readDouble(double low,
double high)
|
double |
readDouble(String prompt)
|
double |
readDouble(String prompt,
double low,
double high)
|
int |
readInt()
|
int |
readInt(int low,
int high)
|
int |
readInt(String prompt)
|
int |
readInt(String prompt,
int low,
int high)
|
String |
readLine()
|
String |
readLine(String prompt)
|
void |
setBackground(Color color)
|
void |
setExceptionOnError(boolean flag)
flag
parameter. |
void |
setFont(Font font)
|
void |
setFont(String str)
str , which is interpreted in the style of
Font.decode . |
void |
setForeground(Color color)
|
void |
setMenuBar(ProgramMenuBar mbar)
|
void |
showErrorMessage(String msg)
|
void |
updateMenuBar(ProgramMenuBar mbar)
|
Field Detail |
---|
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.
Usage: | IOConsole console = new IOConsole(); |
Method Detail |
---|
public void clear()
Usage: | console.clear(); |
public boolean getExceptionOnError()
Usage: | boolean flag = console.getExceptionOnError(); |
Returns: | The current setting of the error-handling mode (false to retry
on errors; true to raise an exception)
|
public BufferedReader getInputScript()
null
.
Usage: | BufferedReader rd = console.getInputScript(); |
Returns: | The reader representing the current input script |
public ProgramMenuBar getMenuBar()
Usage: | ProgramMenuBar mbar = console.getMenuBar(); |
Returns: | The menu bar |
public BufferedReader getReader()
BufferedReader
object that can be used to read
from the console.
Usage: | BufferedReader rd = console.getReader(); |
Returns: | A BufferedReader that reads from this console
|
public PrintWriter getWriter()
PrintWriter
object that can be used to send
output to the console.
Usage: | PrintWriter wr = console.getWriter(); |
Returns: | A PrintWriter that writes to this console
|
public boolean isConsoleMenuItem(JMenuItem item)
true
if the item is one that the console recognizes.
public boolean menuAction(ActionEvent e)
public void print(String value)
print
method is overloaded so that
value
can be of any type.
Usage: | console.print(value); | ||
Parameter: |
|
print
in interface IOModel
public void println()
Usage: | console.println(); |
println
in interface IOModel
public void println(String value)
println
method is
overloaded so that value
can be of any type.
Usage: | console.println(value); | ||
Parameter: |
|
println
in interface IOModel
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.
Usage: | boolean flag = console.readBoolean(); |
Returns: | The value of the input interpreted as a boolean value |
readBoolean
in interface IOModel
public final boolean readBoolean(String prompt)
setExceptionOnError
method.
Usage: | boolean flag = console.readBoolean(prompt); | ||
Parameter: |
| ||
Returns: | The value of the input interpreted as a boolean value |
readBoolean
in interface IOModel
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.
Usage: | boolean flag = console.readBoolean(prompt); | ||||||
Parameters: |
| ||||||
Returns: | The value of the input interpreted as a boolean value |
readBoolean
in interface IOModel
public final double readDouble()
setExceptionOnError
method.
Usage: | double d = console.readDouble(); |
Returns: | The value of the input interpreted as a double
|
readDouble
in interface IOModel
public final double readDouble(double low,
double high)
setExceptionOnError
method.
Usage: | double d = console.readDouble(low, high); | ||||
Parameters: |
| ||||
Returns: | The value of the input interpreted as a double
|
readDouble
in interface IOModel
public final double readDouble(String prompt)
setExceptionOnError
method.
Usage: | double d = console.readDouble(prompt); | ||
Parameter: |
| ||
Returns: | The value of the input interpreted as a double
|
readDouble
in interface IOModel
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.
Usage: | d = console.readDouble(prompt, low, high); | ||||||
Parameters: |
| ||||||
Returns: | The value of the input interpreted as a double
|
readDouble
in interface IOModel
public final int readInt()
setExceptionOnError
method.
Usage: | int n = console.readInt(); |
Returns: | The value of the input interpreted as a decimal integer |
readInt
in interface IOModel
public final int readInt(int low,
int high)
setExceptionOnError
method.
Usage: | int n = console.readInt(low, high); | ||||
Parameters: |
| ||||
Returns: | The value of the input interpreted as a decimal integer |
readInt
in interface IOModel
public final int readInt(String prompt)
setExceptionOnError
method.
Usage: | int n = console.readInt(prompt); | ||
Parameter: |
| ||
Returns: | The value of the input interpreted as a decimal integer |
readInt
in interface IOModel
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.
Usage: | int n = console.readInt(prompt, low, high); | ||||||
Parameters: |
| ||||||
Returns: | The value of the input interpreted as a decimal integer |
readInt
in interface IOModel
public final String readLine()
Usage: | String str = console.readLine(); |
Returns: | The next line of input as a String
|
readLine
in interface IOModel
public String readLine(String prompt)
Usage: | String str = console.readLine(prompt); | ||
Parameter: |
| ||
Returns: | The next line of input as a String
|
readLine
in interface IOModel
public void setBackground(Color color)
Usage: | console.setBackground(color); | ||
Parameter: |
|
public 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.
Usage: | console.setExceptionOnError(flag); | ||
Parameter: |
|
public void setFont(Font font)
Usage: | console.setFont(font); | ||
Parameter: |
|
public 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.
Usage: | console.setFont(str); | ||
Parameter: |
|
public void setForeground(Color color)
Usage: | console.setForeground(color); | ||
Parameter: |
|
public void setMenuBar(ProgramMenuBar mbar)
Usage: | console.setMenuBar(mbar); | ||
Parameter: |
|
public void showErrorMessage(String msg)
Usage: | console.showErrorMessage(msg); | ||
Parameter: |
|
showErrorMessage
in interface IOModel
public void updateMenuBar(ProgramMenuBar mbar)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |