所以这就是这笔交易。我正在使用eclipse来编写一个原始的太空飞行代码。当用户按空格时,它将更改为执行不同动画的不同JPanel,然后再次按空格时,它将返回到第一个JPanel。当我从日食中运行它没有任何问题,它运行完美无瑕。然后,当我将其导出为可运行的jar,然后运行它时,按空格不会做任何事情。好像它根本不会让我按空格键。
package com;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class Runner implements KeyListener {
int framerate = 60;
int WIDTH,HEIGHT;
boolean state;
int amountOfPlanets = 1000;
Planet[] Planets;
Window window;
JPanel travel,zoom,display;
public static void main(String[] args){
new Runner();
}
public Runner() {
state = true;
HEIGHT = 1080; WIDTH = 1920;
window = new Window(WIDTH,HEIGHT);
Planets = new Planet[amountOfPlanets];
for (int i = 0; i < Planets.length; i++) {
Planets[i] = new Planet(WIDTH,HEIGHT);
}
travel = new Panel(Planets,WIDTH,HEIGHT);
window.add(travel);
window.addKeyListener(this);
run();
}
public void run() {
Thread thread = new Thread();
while (thread != null) {
update();
repaint();
try {
Thread.sleep(1000/framerate);
} catch (InterruptedException e) {}
}
}
public void update() {
if (state)
((Panel) travel).movePlanets();
else {
if (!((moveToPlanet) zoom).finished()) {
((moveToPlanet) zoom).movePlanets();
}
}
}
public void repaint() {
if (state)
travel.repaint();
else
zoom.repaint();
}
public void keyPressed(KeyEvent k) {
}
public void keyReleased(KeyEvent k) {
if (k.getKeyCode() == KeyEvent.VK_SPACE) {
if (state)
goToPlanet();
else
leavePlanet();
}
}
public void keyTyped(KeyEvent k) {
}
public void goToPlanet() {
Planet[] temp = ((Panel) travel).getPlanets();
zoom = new moveToPlanet(temp,WIDTH,HEIGHT);
temp = null;
window.remove(travel);
window.add(zoom);
window.revalidate();
state=!state;
}
public void leavePlanet() {
Planet[] temp = ((moveToPlanet) zoom).getPlanets();
temp[((moveToPlanet) zoom).p] = new Planet(WIDTH,HEIGHT);
travel = new Panel(temp,WIDTH,HEIGHT);
temp = null;
window.remove(zoom);
window.add(travel);
window.revalidate();
state=!state;
}
}