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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:17:18+00:00 2026-06-01T04:17:18+00:00

I am creating a basic screensaver for a small project for college and I

  • 0

I am creating a basic screensaver for a small project for college and I am having trouble closing the JFrame after I open it. I have removed the outer panel so as to make it look a proper screensaver but I now have to open the Task Manager to close down the program, I want the window to close when I press a button on the keyboard how can I do that?
Thanks in advance.

——-EDIT——–

I have tried the first two methods given but I can’t seem to get them to work properly. Here is my code for the frame so far:

import java.awt.Color;    
import javax.swing.JFrame;    
public class ScreensaverTest

{      
 public static void main( String[] args )       
{          
   JFrame frame = new JFrame( "Screen Saver" );
   frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   frame.setUndecorated(true);
   ScreenSaverJPanel screensaverTestJPanel = new ScreenSaverJPanel();
   frame.add( screensaverTestJPanel );
   frame.setBackground( Color.BLACK );
   frame.setVisible( true );
   frame.setLocation( 0, 0 );
   frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
   }    
}

——–EDIT———

Also I am using the repaint method to move objects around the screen and I want some of them to move at different speed. I am using a random number generator for the positioning so adjusting the numbers isn’t really an option, please help.

  • 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-01T04:17:19+00:00Added an answer on June 1, 2026 at 4:17 am

    There are a lot of examples on how to do this.

    You need to add an eventListener to the frame that listens for keyboard-inputs and then closes the frame.
    Try looking at this: http://www.java2s.com/Code/Java/Swing-JFC/Reacttoframecloseaction.htm

    You will have to replace the event with something keyboard-related, but that is the best way to go I would think..

    Edit:

    To respond to the edit in the post you could do something like this:

    import javax.swing.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    public class Test {
    
        public Test() {
            final JFrame frame = new JFrame("Screen Saver");
            frame.validate();
            frame.setVisible(true);
    
            KeyAdapter listener = new KeyAdapter() {
                @Override public void keyPressed(KeyEvent e) {
                    frame.dispose();
                }
            };
    
            frame.addKeyListener(listener);
        }
    
        public static void main(String[] args) {
            new Test();
        }
    
    }
    

    And voila – that should work. It compiles for me on Java 7.

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

Sidebar

Related Questions

I am creating a basic msi project using installsheild 2012 premier. I have two
After creating a basic REST service, I've have come to the point where it
My current class is planning on creating a basic networked game, but we have
I'm creating a basic database application in WPF, and I have started using the
I have 2 models: class Team(models.Model): Team model for creating a basic team. image
I'm creating a basic student/course script. I have 3 models I'm working with: Student,
Trying to follow this tutorial Walkthrough: Part 1 - Creating a Basic Project System
I'm creating a basic Swing applcation. I have jTextPane and a button. If the
I am creating a basic GUI with the Windows API and I have run
I am creating a basic GUI with the Windows API and I have run

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.