CPS 1 Class and Method Reference
This sheet is a quick guide to most of the class and methods commonly
used in CPS 1.
StringField
Constructors
- public StringField()
- Construct a new StringField without specifying size or content.
- public StringField(String s)
- Construct a new StringField with the String s as its initial
content.
- public StringField(int cols)
- Construct a new StringField where cols is the width (measured
in columns).
- public StringField(String s, int cols)
- Construct a new StringField where s is the initial content
and cols is the initial width in characters.
Methods
- public String getString()
- Returns the String currently displayed in the StringField.
- public void setString(String s)
- Changes the contents of the StringField to s.
- public void addLabel(String label)
- Add a label to the StringField. The label shows up to the left of the
actual field. For best results, call addLabel before the
StringField is displayed on the screen.
- public char getEchoCharacter()
- See what character is being displayed
- public void setEchoCharacter()
- Set the character to be displayed in the field when letters are typed
in. This is useful if the field is supposed to contain a password--you
can set it to display '*' instead of the actual character.
DoubleField
A DoubleField is similar to a StringField except that it works
exclusively for doubles. You can only type numbers into it (and
an optional minus sign at the beginning, and one period). Rather than
the StringField getString() and setString(String) methods, it
has methods called getDouble() and setDouble(double) as
described below.
Constructors
- public DoubleField()
- Construct a new DoubleField without specifying size or content.
- public DoubleField(int cols)
- Construct a new DoubleField where cols is the width (measured
in characters).
- public DoubleField(double d, int cols)
- Construct a new DoubleField where d is the initial content
and cols is the initial width in characters.
Methods
- public double getDouble()
- Returns the double currently displayed in the DoubleField.
- public void setDouble(double d)
- Changes the contents of the DoubleField to d.
- public void addLabel(String label)
- Add a label to the DoubleField. The label shows up to the left of the
actual field. For best results, call addLabel before the
DoubleField is displayed on the screen.
IntField
An IntField is similar to a StringField except that it works
exclusively for ints. You can only type numbers into it (and
an optional minus sign at the beginning). Rather than
the StringField getString() and setString(String) methods, it
has methods called getInt() and setInt(int) as
described below.
Constructors
- public IntField()
- Construct a new IntField without specifying size or content.
- public IntField(int cols)
- Construct a new IntField where cols is the width (measured
in characters).
- public IntField(int i, int cols)
- Construct a new IntField where i is the initial content
and cols is the initial width in characters.
Methods
- public int getInt()
- Returns the int currently displayed in the IntField.
- public void setInt(int i)
- Changes the contents of the IntField to i.
- public void addLabel(String label)
- Add a label to the DoubleField. The label shows up to the left of the
actual field. For best results, call addLabel before the
DoubleField is displayed on the screen.
String
A String is a Java object that represents a series of alphanumeric
characters, for instance, a word. One may use constructors to create a
new String, but it isn't necessary, since Java automatically creates
a new String anywhere it finds a sequence of characters inside
quotation marks.
Constructors
- public String()
- Construct a new String with no contents
- public String(String s)
- Construct a new String containing s.
Methods
- public String substring(int first, int last)
-
- Returns a substring of the String upon which it is invoked,
beginning with the character with index first and containing
all characters up to but not including the character with index last.
- public int length()
- Returns the number of character in the String
- public int indexOf(String s)
- Returns the index number of the first character of s.
If s fails to appear in the String, returns -1.
Button
Constructors
- public Button()
- Constructs a new Button without a label
- public Button(String s)
- Constructs a new Button with the String s as its label.
Methods
- public void setLabel(String s)
- Makes s the Button's label.