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 9257679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:11:55+00:00 2026-06-18T12:11:55+00:00

I’ve done a few other simple GUI projects the past few weeks, without much

  • 0

I’ve done a few other simple GUI projects the past few weeks, without much of any problem but now I am having trouble getting the program to show after compiling. I can’t figure out what might be wrong as there are no errors being thrown and it is in pretty much the same format as other projects I was given for school.

Any guidance on where to look to get the GUI on screen would be appreciated so that I may go through and tweek things I want before I turn this in.

import java.util.Random;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/***********************************************************************
Program Name: ProgramName.java
Programmer's Name: Student Name
Program Description: Describe here what this program will do
***********************************************************************/
public class GuessGame extends JFrame{

    //Declare GUI components
    private JFrame mainFrame;
    private JButton guessButton;
    private JButton exitButton;
    private JTextField guessField;
    private JTextField answerField;
    private JLabel directionsLabel;
    private JLabel guessLabel;
    private JLabel answerLabel;



    public GuessGame(){

        //Initialize window components
        mainFrame = new JFrame("Number Guessing Game");
        exitButton =  new JButton("Exit");
        guessButton = new JButton("Try your luck");
        guessField = new JTextField(4);
        answerField = new JTextField(50);
        guessLabel = new JLabel("What is your guess?");
        answerLabel = new JLabel("Now it is/isn't");
        directionsLabel = new JLabel("Enter a number and then press the" +
                "guess button until you are correct");

        //Build the GUI
        Container c = mainFrame.getContentPane();
        c.setLayout(new FlowLayout());
        c.add(directionsLabel);
        c.add(guessLabel);
        c.add(guessField);
        c.add(answerLabel);
        c.add(answerField);
        c.add(guessButton);
        c.add(exitButton);

        //Set Mnemonics
        guessButton.setMnemonic('G');
        exitButton.setMnemonic('E');

        mainFrame.setSize(450, 300);

        mainFrame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

        //Call the handler methods for specific functions
        GuessButtonHandler ghandler = new GuessButtonHandler();
        guessButton.addActionListener(ghandler);

        ExitButtonHandler ehandler = new ExitButtonHandler();
        exitButton.addActionListener(ehandler);

        FocusHandler fhandler = new FocusHandler();
        guessField.addFocusListener(fhandler);
        answerField.addFocusListener(fhandler);

    }

    //Implement actionListener for the Guess button
    class GuessButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String instring;
            int counter = 0;
            int guess;
            Random rand = new Random();
            int numberToGuess = rand.nextInt(1000);

            instring = guessField.getText();

            guess = Integer.parseInt(instring);
            counter++;

            if (guess == numberToGuess){
                answerLabel = new JLabel("You win! " + 
        "\nThe number was: " + numberToGuess + 
                "\nIt took you " + counter + " tries");
            }
            else if (guess < numberToGuess){
                answerLabel = new JLabel("Too low");
            }
            else if (guess > numberToGuess){
                answerLabel = new JLabel("Too high");
            }           
        }
    }

    //Implement ActionListener for the exit button
    class ExitButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    }

    //Implement focus listener
    class FocusHandler implements FocusListener{
        public void focusGained(FocusEvent e){
            if (e.getSource()== guessField){
                answerField.setText("");
            }
            else if (e.getSource() == answerField){
                guessButton.requestFocus();
            }
        }


        public void focusLost(FocusEvent arg0) {

        }
    }

    //Main to run program, call GuessGame method
    public static void main(String[] args) {
        new GuessGame();
    }

}
  • 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-18T12:11:56+00:00Added an answer on June 18, 2026 at 12:11 pm

    You will want to call setVisible(true) on the JFrame after adding all components to it. The program won’t mathemagically know that you want to to display anything unless you specifically tell it to do so.

    i.e.,

    public GuessGame() {
    
      // Initialize window components
      mainFrame = new JFrame("Number Guessing Game");
    
      // ..... etc....
    
      FocusHandler fhandler = new FocusHandler();
      guessField.addFocusListener(fhandler);
      answerField.addFocusListener(fhandler);
    
      mainFrame.setVisible(true);
    }  
    

    Also, you don’t want to have this class extend JFrame since this is not necessary. You are already using a JFrame in the constructor and so have no need for another JFrame in the class itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I upgraded downgraded to rails 2.3.17 due to the security bugs, but now I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.