当前位置: 代码迷 >> Java相关 >> 为什么我按ESC键,程序不退出??
  详细解决方案

为什么我按ESC键,程序不退出??

热度:283   发布时间:2006-10-03 17:00:15.0
为什么我按ESC键,程序不退出??
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.JFrame;
public class tuxiang extends JFrame implements KeyListener
{
private boolean isRunning;
private ScreenManager screen;
private DisplayMode displayMode=new DisplayMode(800,600,16,0);
public void init()
{
screen=new ScreenManager();
screen.setFullScreen(displayMode,this);
Window window=screen.getFullScreenWindow();
window.addKeyListener(this);

isRunning=true;
}
public void stop()
{
isRunning=false;
}
public void loop()
{
int i=0;
while(isRunning)
{
i++;
System.out.println("执行了"+i+"秒");
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
}
}
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
{
stop();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public BranchGroup createSceneGraph()
{
BranchGroup objroot=new BranchGroup();
BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,.0,.0),100.0);

Background bg=new Background(new Color3f(1.f,1.f,1.f));
bg.setApplicationBounds(bounds);
objroot.addChild(bg);

TransformGroup objTrans=new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objroot.addChild(objTrans);

DirectionalLight light=new DirectionalLight(
new Color3f(1.f,1.f,0.f),new Vector3f(0.f,1.f,-1.f));
light.setInfluencingBounds(bounds);
objroot.addChild(light);

Appearance ap=new Appearance();
Material mat=new Material();
mat.setDiffuseColor(new Color3f(3.f,2.f,1.f));
ap.setMaterial(mat);

objTrans.addChild(new Box(.2f,.2f,.2f,ap));

MouseRotate b=new MouseRotate();
b.setSchedulingBounds(bounds);
b.setTransformGroup(objTrans);
objroot.addChild(b);

objroot.compile();
return objroot;
}
public void run(DisplayMode displayMode)
{
try
{
init();
loop();
}
finally
{
screen.restoreScreen();
}
}
public tuxiang()
{
setLayout(new BorderLayout());
Canvas3D c=new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center",c);
SimpleUniverse u=new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
BranchGroup scene=createSceneGraph();
u.addBranchGraph(scene);
}
public static void main(String arg[])
{
DisplayMode displayMode=new DisplayMode(
800,600,32,DisplayMode.REFRESH_RATE_UNKNOWN);
tuxiang t=new tuxiang();
t.run(displayMode);
}
}
搜索更多相关的解决方案: import  ESC  utils  sun  awt  

----------------解决方案--------------------------------------------------------
这是ScreenManager.java:
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
public class ScreenManager
{
private GraphicsDevice device;
public ScreenManager()
{
GraphicsEnvironment environment=GraphicsEnvironment.getLocalGraphicsEnvironment();
device=environment.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode displayMode,JFrame frame)
{
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
device.setFullScreenWindow(frame);
if(displayMode!=null&&device.isDisplayChangeSupported())
{
try
{
device.setDisplayMode(displayMode);
}
catch(IllegalArgumentException ex)
{
}
frame.setSize(displayMode.getWidth(),displayMode.getHeight());
}
public JFrame getFullScreenWindow()
{
return (JFrame)device.getFullScreenWindow();
}
public int getWidth()
{
Window window=device.getFullScreenWindow();
if(window!=null)
{
return window.getWidth();
}
else
{
return 0;
}
}
public int getHeight()
{
Window window=device.getFullScreenWindow();
if(window!=null)
{
return window.getHeight();
}
else
{
return 0;
}
}
public void restoreScreen()
{
Window window=device.getFullScreenWindow();
if(window!=null)
{
window.dispose();
}
device.setFullScreenWindow(null);
}
}
程序完全可以执行,就是我按ESC键的时候,总是退不出来,应该是在run()函数哪里有问题,但是我改了很多次,都不行,大家帮我改一下

----------------解决方案--------------------------------------------------------
你的键盘输入事件应该注册到顶级窗口类上面才有效]
----------------解决方案--------------------------------------------------------
我只用了全屏式一个的JFrame,没有其他窗口啊?那 版主说一下,怎么才算是注册在顶级窗口上呢,把window.addKeyListener(this)中的"window."去掉么??!!
----------------解决方案--------------------------------------------------------
加在JFrame的引用上
----------------解决方案--------------------------------------------------------
小弟实在菜,还是请版主给我具体改一下把

----------------解决方案--------------------------------------------------------
private boolean isRunning;
改为
private volatile boolean isRunning;试试
----------------解决方案--------------------------------------------------------
不行啊,不但注销不了全屏,而且时间也不停下来,请版主再帮我好好改改,行吗??!!
----------------解决方案--------------------------------------------------------

我看了一下你的代码,你的那个run方法根本就没有返回,它不返回之前,所有的事件都接受不到的.
这种情况,你应该起一个线程去做 run做的事
只要简单地改一下应该就可以了
public void run(DisplayMode displayMode)
{
try
{
init();
loop();
}
finally
{
screen.restoreScreen();
}
}
改为
public void run(DisplayMode displayMode){
new Thread(new Runnable(){
public void run(){
try
{
init();
loop();
}
finally
{
screen.restoreScreen();
}

}

}).start();
}


----------------解决方案--------------------------------------------------------

然后
screen=new ScreenManager();
screen.setFullScreen(displayMode,this);
this.addKeyListener(this);
this.requestFocusInWindow();
isRunning=true;
放到init方法中
public void init(){
screen=new ScreenManager();
screen.setFullScreen(displayMode,this);
this.addKeyListener(this);
this.requestFocusInWindow();
isRunning=true;

}


----------------解决方案--------------------------------------------------------
  相关解决方案