我正在用java开发一个游戏,我正在尝试从不同的类文件加载不同的级别。每个级别的类文件都是一个 jpanel。这个想法是,当 level1 完成时,它会加载一个删除 jpanel level1 并添加 jpanel level2 的方法。但是当我尝试从 level1 加载 level2 时,我得到一个 java.lang.OutOfMemoryError: unable to create new native thread。错误
我有 3 个类文件:
- main:加载jframe和添加jpanels的方法
- level1:加载level1,然后通过main中的方法加载level2
- 级别2:加载级别2
这是主类中的方法:
public static void levelChanger(int currentMap){
map1 map1 = new map1(null);
map2 map2 = new map2(null);
if(currentMap == 1){
frame.add(map1);
frame.validate();
}else if(currentMap == 2){
frame.remove(map1);
frame.add(map2);
frame.validate();
}
}
这就是我从level1调用它的方式:
mainScreen.levelChanger(2);
希望我提供了足够的信息。谢谢!
这是整个主类:
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class mainScreen{
static JFrame frame = new JFrame("Tile System");
//Key variables
public static int keyUp = KeyEvent.VK_UP;
public static int keyDown = KeyEvent.VK_DOWN;
public static void main(String[] args) {
levelChanger(1);
frame.setSize ( 800, 600 );
frame.setResizable ( false );
frame.setLocationRelativeTo ( null );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setVisible ( true );
frame.setBackground(new Color(135, 206, 250));//RGB color code
frame.setFocusable(true);
new map1(null);
}
public static void exit(){
WindowEvent wev = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
public static void levelChanger(int currentMap){
public static map1 map1 = new map1(null);
public static map2 map2 = new map2(null);
if(currentMap == 1){
frame.add(map1);
frame.validate();
}else if(currentMap == 2){
frame.remove(map1);
frame.add(map2);
frame.validate();
}
}
}