Question

Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

Modify Java Code below:

- Remove Button Try the Number

-Instead of Try the Number button just hit enter on keyboard to try number

-Remove Button Quit

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

// Main Class
public class GuessGame extends JFrame {

// Declare class variables
private static final long serialVersionUID = 1L;
public static Object prompt1;
private JTextField userInput;
private JLabel comment = new JLabel(" ");
private JLabel comment2 = new JLabel(" ");
private int randomNumber;
private int counter = 0;

// Constructor
public GuessGame() {
super("Guessing Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Content pane
setLayout(new FlowLayout());
Container c = getContentPane();

// Create components
JButton guessButton = new JButton("Try the number");
JButton newGameButton = new JButton("New Game");
JButton quitButton = new JButton("Quit");
JLabel prompt1 = new JLabel("I have a number between 1 and 1000.");
JLabel prompt2 = new JLabel("Can you guess the number?");
JLabel prompt3 = new JLabel("Please enter your guess: ");
comment = new JLabel(" ");
comment2 = new JLabel(" ");
userInput = new JTextField(5);

// Adding components to the pane
c.add(prompt1);
c.add(prompt2);
c.add(prompt3);
c.add(userInput);
c.add(guessButton);
c.add(newGameButton);
c.add(quitButton);
c.add(comment);
c.add(comment2);

// Set the mnemonic
guessButton.setMnemonic('T');
newGameButton.setMnemonic('N');
quitButton.setMnemonic('Q');

// Format pane
setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);

initializeNumber();

// Create the button handlers
GuessButtonHandler ghandler = new GuessButtonHandler(); // instantiate
// new object
guessButton.addActionListener(ghandler); // add event listener

NewButtonHandler nhandler = new NewButtonHandler();
newGameButton.addActionListener(nhandler);

QuitButtonHandler qhandler = new QuitButtonHandler();
quitButton.addActionListener(qhandler);
} // End constructor

//
private void initializeNumber() {
randomNumber = new Random().nextInt(1000) + 1;
}

// GuessButton inner class
private class GuessButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {

// Declare class variables
int getUserInput;
int diff;
int Difference;

// Validate input and if statements for user input
try {
getUserInput = Integer.parseInt(userInput.getText().trim());
counter++;

if (getUserInput == randomNumber) {
JOptionPane.showMessageDialog(null, "Correct! It took you "
+ counter + " guesses", "Random Number: "
+ randomNumber, JOptionPane.INFORMATION_MESSAGE);
initializeNumber();
return;
}
if (getUserInput > randomNumber) {
comment2.setText("The guess was too HIGH. Try a lower number.");
comment2.setForeground(Color.WHITE);
diff = getUserInput - randomNumber;
Difference = Math.abs(diff);
} else {
comment2.setText("The guess was too LOW. Try a higher number.");
comment2.setForeground(Color.WHITE);
diff = randomNumber - getUserInput;
Difference = Math.abs(diff);
}

if (Difference >= 30) {
comment.setText("You are Cold. ");
comment.setForeground(Color.WHITE);
GuessGame.this.setBackgroundColor(Color.BLUE);
}

if (Difference <= 15) {
comment.setText("You are getting Warm");
comment.setForeground(Color.WHITE);
GuessGame.this.setBackgroundColor(Color.RED);
}
} catch (NumberFormatException ex) {
comment.setText("Enter a VALID number!");
}
}
} // End GuessButtonHandler

// NewButton inner class
private class NewButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
GuessGame app = new GuessGame();

}
} // End NewButtonHandler

// QuitButton inner class
private class QuitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} // End QuitButtonHandler

// Setting background color
private void setBackgroundColor(Color WHITE) {
getContentPane().setBackground(WHITE);
}

// Main method
public static void main(String args[]) {
// instantiate GuessGame object
GuessGame app = new GuessGame();

}// End main method
}// End main class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the updated code below:

package classess5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

// Main Class
public class GuessGame extends JFrame {

   // Declare class variables
   private static final long serialVersionUID = 1L;
   public static Object prompt1;
   private JTextField userInput;
   private JLabel comment = new JLabel(" ");
   private JLabel comment2 = new JLabel(" ");
   private int randomNumber;
   private int counter = 0;

   // Constructor
   public GuessGame() {
       super("Guessing Game");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       // Content pane
       setLayout(new FlowLayout());
       Container c = getContentPane();

       // Create components
   //   JButton guessButton = new JButton("Try the number");
       JButton newGameButton = new JButton("New Game");
   //   JButton quitButton = new JButton("Quit");
       JLabel prompt1 = new JLabel("I have a number between 1 and 1000.");
       JLabel prompt2 = new JLabel("Can you guess the number?");
       JLabel prompt3 = new JLabel("Please enter your guess: ");
       comment = new JLabel(" ");
       comment2 = new JLabel(" ");
       userInput = new JTextField(5);

       // Adding components to the pane
       c.add(prompt1);
       c.add(prompt2);
       c.add(prompt3);
       c.add(userInput);
   //   c.add(guessButton);
       c.add(newGameButton);
   //   c.add(quitButton);
       c.add(comment);
       c.add(comment2);

       // Set the mnemonic
//       guessButton.setMnemonic('T');
       newGameButton.setMnemonic('N');
//       quitButton.setMnemonic('Q');

       // Format pane
       setSize(300, 200);
       setLocationRelativeTo(null);
       setVisible(true);
       setResizable(false);

       initializeNumber();

       // Create the button handlers
       GuessButtonHandler ghandler = new GuessButtonHandler(); // instantiate
       // new object
//       guessButton.addActionListener(ghandler); // add event listener
       userInput.addActionListener(ghandler);
       NewButtonHandler nhandler = new NewButtonHandler();
       newGameButton.addActionListener(nhandler);

       QuitButtonHandler qhandler = new QuitButtonHandler();
//       quitButton.addActionListener(qhandler);
   } // End constructor

   //
   private void initializeNumber() {
       randomNumber = new Random().nextInt(1000) + 1;
   }

   // GuessButton inner class
   private class GuessButtonHandler implements ActionListener {
       public void actionPerformed(ActionEvent e) {

           // Declare class variables
           int getUserInput;
           int diff;
           int Difference;

           // Validate input and if statements for user input
           try {
               getUserInput = Integer.parseInt(userInput.getText().trim());
               counter++;

               if (getUserInput == randomNumber) {
                   JOptionPane.showMessageDialog(null, "Correct! It took you "
                           + counter + " guesses", "Random Number: "
                                   + randomNumber, JOptionPane.INFORMATION_MESSAGE);
                   initializeNumber();
                   return;
               }
               if (getUserInput > randomNumber) {
                   comment2.setText("The guess was too HIGH. Try a lower number.");
                   comment2.setForeground(Color.WHITE);
                   diff = getUserInput - randomNumber;
                   Difference = Math.abs(diff);
               } else {
                   comment2.setText("The guess was too LOW. Try a higher number.");
                   comment2.setForeground(Color.WHITE);
                   diff = randomNumber - getUserInput;
                   Difference = Math.abs(diff);
               }

               if (Difference >= 30) {
                   comment.setText("You are Cold. ");
                   comment.setForeground(Color.WHITE);
                   GuessGame.this.setBackgroundColor(Color.BLUE);
               }

               if (Difference <= 15) {
                   comment.setText("You are getting Warm");
                   comment.setForeground(Color.WHITE);
                   GuessGame.this.setBackgroundColor(Color.RED);
               }
           } catch (NumberFormatException ex) {
               comment.setText("Enter a VALID number!");
           }
       }
   } // End GuessButtonHandler

   // NewButton inner class
   private class NewButtonHandler implements ActionListener {
       public void actionPerformed(ActionEvent e) {
           GuessGame app = new GuessGame();

       }
   } // End NewButtonHandler

   // QuitButton inner class
   private class QuitButtonHandler implements ActionListener {
       public void actionPerformed(ActionEvent e) {
           System.exit(0);
       }
   } // End QuitButtonHandler

   // Setting background color
   private void setBackgroundColor(Color WHITE) {
       getContentPane().setBackground(WHITE);
   }

   // Main method
   public static void main(String args[]) {
       // instantiate GuessGame object
       GuessGame app = new GuessGame();

   }// End main method
}// End main class

output:

Add a comment
Know the answer?
Add Answer to:
Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • Can you help me to link two frames in Java Swing and then if you click...

    Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :) First frame code: //First import javax.swing.*; import java.awt.event.*; import java.io.*; public class Sample extends JFrame implements ActionListener { JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; JButton proceed= new JButton("Proceed"); Sample() { open = new JMenuItem("Open File"); open.addActionListener(this); file = new JMenu("File"); file.add(open); mb = new JMenuBar(); mb.setBounds(0, 0, 400, 20); mb.add(file); ta...

  • I tried to add exception handling to my program so that when the user puts in...

    I tried to add exception handling to my program so that when the user puts in an amount of change that is not the integer data type, the code will catch it and tell them to try again, but my try/catch isnt working. I'm not sure how to fix this problem. JAVA code pls package Chapter9; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIlab extends JFrame implements ActionListener {    private static final int WIDTH = 300;    private...

  • The program below is a GUI hander for a program with the following components. private int...

    The program below is a GUI hander for a program with the following components. private int count = 0; private JLabel label; private JButton button; The handler below is for the botton. Briefly summarize what happens when the user clicks the button. public class ButtonListerner implements ActionListener {public void actionPerformed (ActionEvent e) {count ++; if (count X2 == 0) {contentPame, setBackground(color. red); label. setText ("Go");}} if (count > 10} {button .SetEnabled (false); label, setText ("ARRIVED"); contentpane. setBackground (Color. gray);}}}

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically look...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;    private boolean...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;   ...

  • Debug this java and post the corrected code. /* Creates a simple JPanel with a single...

    Debug this java and post the corrected code. /* Creates a simple JPanel with a single "quit" JButton * that ends the program when clicked. * There are 3 errors, one won't prevent compile, you have to read comments. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuitIt extends JFrame {     public QuitIt() {         startIt(); //Create everything and start the listener     }     public final void startIt() {        //Create the JPanel        JPanel myPanel =...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT