[英]Closing a JFrame using the keyboard
I am creating a basic screensaver for a small project for college and I am having trouble closing the JFrame after I open it. 我正在为一个大学的小项目创建一个基本的屏幕保护程序,打开JFrame后我无法关闭它。 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. 另外,我正在使用repaint方法在屏幕上移动对象,并且我希望其中一些对象以不同的速度移动。 I am using a random number generator for the positioning so adjusting the numbers isn't really an option, please help.
我正在使用随机数生成器进行定位,因此调整数字并不是真正的选择,请提供帮助。
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. 您需要向框架添加一个eventListener,以监听键盘输入,然后关闭该框架。 Try looking at this: http://www.java2s.com/Code/Java/Swing-JFC/Reacttoframecloseaction.htm
尝试看一下: 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.
它在Java 7上为我编译。
Here is the code if you have only a single JFrame. 如果您只有一个JFrame,则这里是代码。 You should add it when you initialize your components.
初始化组件时应添加它。
addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(java.awt.event.KeyEvent evt)
{
processWindowEvent(new WindowEvent(getWindows()[0], WindowEvent.WINDOW_CLOSING));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.