所以我制作了一个蛇游戏,它成功了
我想用更多与Java相关的技术来更新它,比如Springboot但当我尝试运行与没有Spring时完全相同的代码时,有人向我抛出了无头异常。
我试图在线搜索,但没有找到任何有用的信息
知道是什么原因吗?
最初的主要方法:
package run;
import game.GameFrame;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
new GameFrame();
}
}
原始GameFrame:
package game;
import javax.swing.*;
public class GameFrame extends JFrame {
public GameFrame(){
this.add(new GamePanel());
this.setTitle("Kevin The Snake");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
"Springboot"的主要方法:
package app.core;
import static app.core.statics.Globals.*;
import app.core.game.GameFrame;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WiseKevinApplication {
public static void main(String[] args) {
SpringApplication.run(WiseKevinApplication.class, args);
new GameFrame(); // TODO -- Check exceptions (Headless Exception) and maybe try launching GameFrame as an entity
}
}
"Springboot"游戏框架:
package app.core.game;
import javax.swing.*;
public class GameFrame extends JFrame {
public GameFrame(){
this.add(new GamePanel());
this.setTitle("Wise Kevin");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
您呼叫"新的";您的GameFrame不在Spring的控制之下。您需要使用Spring配置创建该实例。
我建议你读这篇文章。