Instance Variables
The JOptionPane Class The JOptionPane Class displays a dialog for user information and • interaction... ... it is part of the javax.swing package and must be imported before you • can use it. import javax.swing.JOptionPane; public class Test { public static void main( String[] args ) { JOptionPane.showMessageDialog( null, "Hello World" ); } }
showMessageDialog This method displays a message and an OK button. void JOptionPane.showMessageDialog( parent, message ) parent This is the parent window for the dialog; for us it will always be null . message The message to display. public static void main( String[] args ) { JOptionPane.showMessageDialog( null, "Hello World" ); }
showInputDialog (1) Invoked as shown below, this method displays a message, OK and Cancel buttons, and a place for a user to enter input. Input from the user is returned to the caller. String JOptionPane.showInputDialog( parent, message ) parent: This is the parent window for the dialog; for us it will always be null . message: The message to display. Returns: OK button selected: the user's input; Cancel button selected: null JOptionPane.showInputDialog( null, message );
showInputDialog (1) Example import javax.swing.JOptionPane; public class Test { public static void main( String[] args ) { String message = "Please enter your name"; String name = JOptionPane.showInputDialog( null, message ); if ( name != null ) System.out.println( "The user's name is: " + name ); else System.out.println( "no name entered" ); } }
showInputDialog (2) This form of the showInputDialogMethod displays a message, OK and Cancel buttons, and allows you to specify the title and type of the dialog. String JOptionPane.showInputDialog(parent, message, title, type ) JOptionPane.showInputDialog( null, message, "Missing Data", JOptionPane.WARNING_MESSAGE );
showInputDialog (2) Detail title: This string will be displayed in the dialog’s title bar. type: This integer must be a constant from the JOptionPane class: JOptionPane.ERROR_MESSAGE, JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE, JOptionPane.QUESTION_MESSAGE JOptionPane.PLAIN_MESSAGE JOptionPane.showInputDialog( null, message, "Missing Data", JOptionPane.WARNING_MESSAGE );
showInputDialog (2) Example import javax.swing.JOptionPane; public class Test { public static void main( String[] args ) { String message = "You must enter your name to continue"; String name = JOptionPane.showInputDialog( null, message, "Missing Data", JOptionPane.WARNING_MESSAGE ); if ( name == null ) System.out.println( "Dialog canceled" ); else if ( name.equals( "" ) ) System.out.println( "no name entered" ); else System.out.println( "The user's name is: " + name ); } }
showConfirmDialog This form of the showConfirmDialogMethod displays a message, yes , no and cancel buttons, and allows you to specify the title and type of the dialog. String JOptionPane.showConfirmDialog(parent, message ) parent: This is the parent window for the dialog; for us it will always be null . message: The message to display. Returns: JOptionPane.YES_OPTION, JOptionPane.NO_OPTION or JOptionPane.CANCEL_OPTION JOptionPane.showConfirmDialog( null, message );
showConfirmDialog Example import javax.swing.JOptionPane; public class Test { public static void main( String[] args ) { String message = "Are you sure you want to do this?"; int response = JOptionPane.showConfirmDialog( null, message ); if ( response == JOptionPane.YES_OPTION ) System.out.println( "the user is sure" ); else if ( response == JOptionPane.NO_OPTION ) System.out.println( "the user is not sure" ); else if ( response == JOptionPane.CANCEL_OPTION ) System.out.println( "the user has cancelled" ); else System.out.println( "the user selected no option" ); } }
JOptionPane Documentation For complete documentation of the many JOptionPane alternatives, see the javax.swing documentation.
Exercises 1. Textbook, Chapter 4, page 4 ‐ 5 Exercise 4.1
Instance Variables An instance variable is used by an object to store a value unique to that • object. Instance variables are declared at the class level. • Instance variables are almost always private . • public class Name { private String firstName_ = null; // instance variable private String lastName_ = null; // instance variable public void someMethod() { ... } }
Instance Variables Example (1) Below are some of the instance variable declarations for Turtle objects. public class Turtlet extends Object { private double heading = 0; // heading initially east private double xcor; // current x position of Turtle private double ycor; // current position of Turtle private Color currColor; // current color of Turtle ... } Every Turtle you create has a unique set of these variables.
Instance Variables Example (2) Fred: public class Test xco = -128 { yco = 128 public static void main( String[] args ) color = red { Turtle fred = new Turtle(); Turtle wilma = new Turtle(); fred.move( 135, 128 * Math.sqrt( 2 ) ); fred.switchTo( Turtle.RED ); wilma.move( -45, 128 * Math.sqrt( 2 ) ); wilma.switchTo( Turtle.GREEN ); fred.fillBox( 64,64 ); Wilma: wilma.fillCircle( 128 ); xco = 128 yco = -128 } color = green }
Accessor Methods (1) public class Name { private String firstName_ = null; private String lastName_ = null; When instance variables are public void setFirstName( String name ) private, you often { employ accessors firstName_ = name; to set and/or get } their values. public String getFirstName() { return firstName_; } // public void setLastName( String name ) // public String getLastName( String name ) ...
Accessor Methods (2) ... public void setName( String first, String last ) { firstName_ = first; lastName_ = last; } Sometimes accessors are public String getName() created just for { convenience. return lastName_ + ", " + firstName_; } }
Using Accessor Methods public class Test { public static void main( String[] args ) { Output: Name person1 = new Name(); thomas person1.setFirstName( "george" ); jefferson person1.setLastName( "washington" ); washington, george Name person2 = new Name(); person2.setName( "thomas", "jefferson" ); System.out.println( person2.getFirstName() ); System.out.println( person2.getLastName() ); System.out.println( person1.getName() ); } }
Exercises 1. Implement the name class as shown in the slides; you will have to write the code for setLastName and getLastName . 2. To the name class add a string instance variable to hold a title (such as Mr . and Ms.) . Write accessors for the new variable.
Basic Game The BasicGame is a class that may serve as the core of many different games. We will start by implementing a simple guessing game. It has two instance variables, one for holding the word to guess, and one for holding the user’s guess. public class BasicGame { private String secretWord_ = "duck"; private String usersGuess_ = "none"; . . .
Basic Game: playOneGame The playOneGame method: 1. Asks the player’s first guess. 2. If the guess is correct, displays a congratulatory message and stops. 3. If the guess is wrong, displays an error message and returns to step 2. . . . public void playOneGame() { askUsersFirstChoice(); while ( shouldContinue() ) { showUpdatedStatus(); askUsersNextChoice(); } showFinalStatus(); } . . .
Basic Game: playManyGames The playManyGames method: 1. Plays one game. 2. Asks the user if she wants to play another. 3. Plays another game if the user says yes, otherwise stops. . . . public void playManyGames() { int again = 0; do { playOneGame(); again = JOptionPane.showConfirmDialog( null, "again?" ); } while ( again == JOptionPane.YES_OPTION ); } . . .
Basic Game: askUsersFirstChoice The askUsersFirstChoice method: 1. Asks the user to enter a string, which is stored in the object’s usersGuess_ instance variable. . . . public void askUsersFirstChoice() { usersGuess_ = JOptionPane.showInputDialog( null, "Guess the secret word" ); } . . .
Basic Game: askUsersNextChoice The askUsersNextChoice method: 1. Just calls askUsersFirstChoice . . . . public void askUsersNextChoice() { askUsersFirstChoice(); } . . .
Basic Game: shouldContinue The askUsersNextChoice method: 1. Compares the user's guess with the secret word. If they are not equal it returns true, otherwise it returns false. . . . public boolean shouldContinue() { boolean rval = !secretWord_.equals( usersGuess_ ); return rval; } . . . If the user guesses right we don’t want to continue, so return false.
Basic Game: showUpdatedStatus The showUpdatedStatus method: 1. Displays an error message, and gives the user a hint about the correct answer. . . . public void showUpdatedStatus() { String message = "That was wrong. Hint: it quacks."; JOptionPane.showMessageDialog( null, message ); } . . .
Basic Game: showFinalStatus The showFinalStatus method: 1. Displays a congratulatory message. . . . public void showFinalStatus() { String message = "That was right, congratulations."; JOptionPane.showMessageDialog( null, message ); } . . .
Recommend
More recommend