import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GuessNum2 { // Define Event Listener for Enter button private static class EnterAction implements ActionListener { public void actionPerformed(ActionEvent event) { ourRandom = (int) (Math.random ( ) * 10.0 ) + 1; // If the user correctly entered // the same number as our random number if ( Double.parseDouble(userInputField.getText()) == ourRandom) { // Display text response informing user of success // (optional HTML tags) response.setText( "" + "That's right! You correctly guessed my number: " + ourRandom + ". Select the Play Again button to play again." + "" + ""); } // Else the numbers do not match else { // Display text response stating user's number did not match response.setText("" + "Sorry, that is not the correct number." + " I was thinking of " + "number " + ourRandom + ". Select the Play Again button to " + "play again." + ""); } } } // Define Event Listener for Play Again button. Resets fields. private static class PlayAgainAction implements ActionListener { public void actionPerformed(ActionEvent event) { // Blank out user input data field userInputField.setText(""); // Blank out application response message field response.setText(""); } } // The following fields need // to be accessible to method(s) above, so // define them as static (class) variables. private static JTextField userInputField; // Holds number entered by user private static JLabel response; // Holds text response to voter action private static String userNum; // Holds actual user vote private static int ourRandom; // Our random number generated public static void main(String[] args) throws IOException { // Instantiate new JFrame object. Title for frame is optional. JFrame ourFrame=new JFrame("The Number Guessing Game"); ourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ourFrame.setSize(800,300); // Get address of ourFrame's pane and set up appearance Container ourPane=ourFrame.getContentPane(); ourPane.setLayout(new GridLayout(3,3)); // Instantiate user instruction text. // Welcome user and explain game. JLabel instruction=new JLabel( "" + "WELCOME TO THE NUMBER GUESSING GAME!
" + "
I'm thinking of an integer between 1 and 10. " + "Guess the number by typing it in the space to the " + "right and pressing the Enter button." + "
"); // Instantiate field for user // to type in integer between 1 and 10 userInputField = new JTextField("", 2); // Create button for user to click // in order to enter (submit) integer JButton enterButton=new JButton( "Enter"); // Create button to reset fields // in preparation for playing again JButton playAgainButton=new JButton( "Play Again"); // Register an actionlistener with the Enter button EnterAction ourEnterListener=new EnterAction(); enterButton.addActionListener(ourEnterListener); // Register an actionlistener with the PlayAgain button PlayAgainAction ourAgainListener=new PlayAgainAction(); playAgainButton.addActionListener(ourAgainListener); // Instantiate text response field and initialize to blank. response=new JLabel("", JLabel.CENTER); // Place components in frame and make it visible ourPane.add(instruction); ourPane.add(userInputField); ourPane.add(playAgainButton); ourPane.add(enterButton); ourPane.add(response); ourFrame.setVisible(true); } }