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

  • Home
  • SEARCH
  • 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 9016263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:57:02+00:00 2026-06-16T03:57:02+00:00

I’ve written a GUI program and I’m not sure what I’m doing wrong. When

  • 0

I’ve written a GUI program and I’m not sure what I’m doing wrong. When I run the program, it just quits almost instantly. Hope you can help, here’s the program:

public class RandomGame extends JFrame {
    private JTextField t1 = new JTextField();
    private double t1num = Double.parseDouble(t1.getText());


    public RandomGame() {
        setLayout(new FlowLayout());
        Event1 e1 = new Event1();
        t1.addKeyListener(e1);
        add(t1);

    }


    public class Event1 implements KeyListener {
        @Override
        public void keyPressed(KeyEvent arg0) {
            Random r = new Random();
            switch (arg0.getKeyCode()) {
            case KeyEvent.VK_ENTER:
                if(t1num == r.nextInt()) {
                    t1.setText("You Won!");
                }
                else {
                    t1.setText("You Lost.");
                }

                }
            }



        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }
    }
    public static void main(String[] args) {
        RandomGame gui = new RandomGame();
        gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
        gui.setSize(1280, 800);
        gui.setTitle("Random Game");
        gui.setVisible(true);

    }
}

(Don’t worry about the imports, I just haven’t included them)
Any help would be appreciated.

  • 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-16T03:57:04+00:00Added an answer on June 16, 2026 at 3:57 am

    A NumberFormatException is thrown on startup on this line:

    private double t1num = Double.parseDouble(t1.getText());
    

    as the JTextField t1 will contain an empty String.

    In fact, this can be simply left unassigned and will take the default 0 value used for numeric primitives.


    Additionally when using Random

    t1num == r.nextInt();
    

    could be written as:

    t1num == r.nextDouble();
    

    Also KeyListener is not recommended for use with JTextComponents. Here you simply require the ENTER key action to be handled so an ActionListener would be better & simpler to use.

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

Sidebar

Related Questions

I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT

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.