Java 创建多关卡游戏时如何重新启动JFrame?
我有一个一级的电脑游戏,我创造了,我想添加另一个级别 主要内容如下:Java 创建多关卡游戏时如何重新启动JFrame?,java,swing,jframe,frame,restart,Java,Swing,Jframe,Frame,Restart,我有一个一级的电脑游戏,我创造了,我想添加另一个级别 主要内容如下: public class Main extends JDialog { private static final long serialVersionUID = 1L; protected static TimerThread timerThread; static JStatusBar statusBar = new JStatusBar(); private static JFram
public class Main extends JDialog
{
private static final long serialVersionUID = 1L;
protected static TimerThread timerThread;
static JStatusBar statusBar = new JStatusBar();
private static JFrame frame;
private static final int FRAME_LOCATION_X = 300;
private static final int FRAME_LOCATION_Y = 50;
private static final int FRAME_SIZE_X = 850; // animator's target frames per second
private static final int FRAME_SIZE_Y = 700; // animator's target frames per second
private static final String WorldName = "FPS 2013 CG Project";
private static final String HARD_TARGET = "src/res/target.jpg";
private static final String runningOut = "Time is running out - you have : ";
static int interval;
static Timer timer1;
static JLabel changingLabel1 = null;
/**
* NEW
*/
private static Timer timer;
private static int count = 60;
private static ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
count--;
if (count == 0)
timer.stop();
changingLabel1.setText(runningOut + count + " seconds");
}
};
public static void exitProcedure() {
timerThread.setRunning(false);
System.exit(0);
}
/**
* Clock timer1
* @author X2
*
*/
public static class TimerThread extends Thread
{
protected boolean isRunning;
protected JLabel dateLabel;
protected JLabel timeLabel;
protected SimpleDateFormat dateFormat =
new SimpleDateFormat("EEE, d MMM yyyy");
protected SimpleDateFormat timeFormat =
new SimpleDateFormat("h:mm a");
public TimerThread(JLabel dateLabel, JLabel timeLabel) {
this.dateLabel = dateLabel;
this.timeLabel = timeLabel;
this.isRunning = true;
}
@Override
public void run() {
while (isRunning) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Calendar currentCalendar = Calendar.getInstance();
Date currentTime = currentCalendar.getTime();
dateLabel.setText(dateFormat.format(currentTime));
timeLabel.setText(timeFormat.format(currentTime));
}
});
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
}
}
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
frame = new JFrame(WorldName);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
/**
* the timer of the count-down
*/
timer = new Timer(1000, timerAction);
timer.start();
changingLabel1 = new JLabel(runningOut);
statusBar.setLeftComponent(changingLabel1);
final JLabel dateLabel = new JLabel();
dateLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(dateLabel);
final JLabel timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(timeLabel);
contentPane.add(statusBar, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
timerThread = new TimerThread(dateLabel, timeLabel);
timerThread.start();
Renderer myCanvas = new Renderer();
final Animator animator = new Animator(myCanvas);
Toolkit t = Toolkit.getDefaultToolkit();
BufferedImage originalImage = null;
try
{
originalImage = ImageIO.read(new File(HARD_TARGET));
}
catch (Exception e1) {e1.printStackTrace();}
Cursor newCursor = t.createCustomCursor(originalImage, new Point(0, 0), "none");
frame.setCursor(newCursor);
frame.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
frame.add(myCanvas);
frame.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
new Thread()
{
@Override
public void run()
{
animator.stop();
System.exit(0);
}
}.start();
}
});
frame.setVisible(true);
animator.start();
myCanvas.requestFocus();
myCanvas.setFocusable(true);
}
});
}
}
此主函数使用类渲染器,即
class Renderer extends GLCanvas implements GLEventListener, KeyListener ,MouseListener ,MouseMotionListener {...}
这个班掌握着比赛的第一关
如您所见,我还使用了JFrame
和jogl1.0
我的问题是:在我完成第一关后,如何重置JFrame?显然我不能使用System.exit(0)代码>,因为它将退出整个程序
我想要的是转到另一个拥有第二级的班级
如果不使用System.exit(0)退出,我如何做到这一点代码>
谢谢通过使用,您可以有效地停止面板。然后只需创建一个新的JFrame
和add()
即可。考虑使用<代码> jFrase>代码>创建它自己的函数,这样你就不必继续重写它。 你可以使用<代码>框架.Debug()>代码>然后创建与下一级相同的JFrror…< /P>
我建议你重新构造你的代码…你的主类应该只包含Main方法,你应该从哪里开始游戏,它将位于另一个包含JFrame和Thread的类中…为什么不在完成第一个级别时给它一个新的渲染器呢?只是一个建议,试着在每个级别使用JPanel。当一个级别完成时,您可以向JFrame发送一些事件以更改为下一个级别的JPanel。