Closing a JFrame using the keyboard

joppz

New Member
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:\[code\]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); } }\[/code\]--------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.
 
Back
Top