我正在使用java制作一个简单的游戏。我有每秒四十次的代码,运行屏幕线程。但是,每当我调用屏幕线程时,都会打开一个新窗口,旧窗口也会保持打开状态。有没有办法解决这个问题?感谢
package sylvyrfysh;
import java.awt.*;
import javax.swing.*;
import sylvyrfysh.handlers.OreHandler;
import sylvyrfysh.troops.manager.TroopManager;
public class Screen extends JFrame implements Runnable{
private static final long serialVersionUID = -377919050238217712L;
public Screen(){
super("Commander");
setContentPane(new DrawPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setFont(new Font("Arial",Font.PLAIN,12));
setSize(607,669);
setVisible(true);
setResizable(false);
KeyboardFocusManager manager=KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new EventHandler());
addMouseListener(new MouseHandler());
}
class DrawPane extends JPanel{
private static final long serialVersionUID = 2729138328693556841L;
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0,0,600,40);
g.setColor(Color.CYAN);
g.drawString("Ore: "+Main.ore,0,10);
g.drawString("Base Health: "+Main.baseHealth,0,30);
g.setColor(Color.BLUE);
g.fillRect(0,40,600,600);
g.setColor(Color.GREEN);
g.fillRect(0,40,100,100);
for(int[] i:TroopManager.troops){
g.fillRect(i[1],i[2]+40,1,1);
}
g.setColor(Color.RED);
g.fillRect(500, 540, 100, 100);
for(int[] i:TroopManager.enemyTroops){
g.fillRect(i[1],i[2]+40,1,1);
}
g.setColor(Color.YELLOW);
for(int[] i:OreHandler.ore){
g.fillRect(i[1],i[2]+40,1,1);
}
}
}
public void run(){repaint();}
}
编辑:似乎正在发生的事情是每当我调用屏幕线程时,它也会调用构造函数。