Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:56:53+00:00 2026-06-10T09:56:53+00:00

I’m new in Java and I need your help in implementing a GUI. Below

  • 0

I’m new in Java and I need your help in implementing a GUI. Below is a Guessing Game code. It works.

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

  public class GuessGame extends JFrame {
     private JTextField guessTextField;
     private JLabel introLabel, guessLabel, clueLabel;
     private JButton enterB, playAgainB;

     private int randomNumber;

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

        //Creates components
        enterB = new JButton("Guess");
        playAgainB = new JButton("Quit");
        introLabel = new JLabel("I have a number between 1 and 1000.");
        guessLabel = new JLabel("Can you guess my number? Please enter your guess:");

        clueLabel = new JLabel("");
       // comment2 = new JLabel(" ");
        guessTextField = new JTextField(5);

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

        //adding component to the pane
        c.add(introLabel);
        c.add(guessLabel);
        c.add(guessTextField);
        //c.add(comment2);
        c.add(enterB);
        c.add(playAgainB);
        c.add(clueLabel);

        //enterB.setMnemonic('G');
        //playAgainB.setMnemonic('Q');

        setSize(350, 200);
        setLocationRelativeTo(null);
        setVisible(true);
        //setResizable(false);

        initializeNumber();

        //creating the handler
        GuessButtonHandler ghandler = new GuessButtonHandler(); //instantiate new object
        enterB.addActionListener(ghandler); // add event listener

        QuitButtonHandler qhandler = new QuitButtonHandler();
        playAgainB.addActionListener(qhandler);
     }

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

     class QuitButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
           System.exit(0);
        }
     }

     class GuessButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
           int getUserInput;
           int diff;
           int Difference;
           try {
              getUserInput = Integer.parseInt(guessTextField.getText().trim());      

              if (getUserInput == randomNumber) {
                  clueLabel.setText("                              Correct!");
              }
              if (getUserInput > randomNumber) {
                 clueLabel.setText("                           Too High");
              } else {
                 clueLabel.setText("                           Too Low");
              }


           } 
           catch (NumberFormatException e1) {
              clueLabel.setText("Enter a VALID number!");
           }
        }


     }



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

     }
  }

However, the color of the window should change into red or blue. Please help me with this code. I’m new in Java and it’s syntax. I’d really appreciate your help. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:56:55+00:00Added an answer on June 10, 2026 at 9:56 am

    You could use:

    current = Integer.parseInt(guessTextField.getText().trim()); 
    if (!firstTime) {
        if (getUserInput > previous) {
            getContentPane().setBackground(Color.red);
        } else {
            getContentPane().setBackground(Color.blue);
        }
    }
    

    where firstTime and previous are class member variables.

    Don’t forget to assign previous if the getUserInput == randomNumber is not met.

    Update:

    You are setting the background blue twice:

    if (getUserInput < randomNumber) {
       clueLabel.setText("Too Low");
       getContentPane().setBackground(Color.blue); <------ remove this extra call
       previous = getUserInput;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.