问题描述
我有一个我创建的一级计算机游戏,我想添加另一个级别.
这是主要的:
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和JOGL 1.0.
我的问题是:完成第一级后如何重置Jframe?显然,我不能使用System.exit(0);,因为它会退出整个程序.
我想要的是移至另一个拥有第二级的课程.
如果不使用System.exit(0);退出,我该怎么做?
谢谢
推荐答案
通过使用 remove() ,您可以有效地停止面板.然后只需创建一个新的JFrame和add()即可.考虑制作JFrame创建自己的功能,这样您就不必继续重写.
其他推荐答案
好吧,您可以使用frame.dispose()然后使用下一个级别创建相同的Jframe ...
我建议您重组代码...您的主要类应仅包含主方法,然后您应该从哪里开始游戏,该方法将位于包含jframe和thread的另一类中...
问题描述
I have a one level computer game that I created , and I want to add another level .
Here is the Main :
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); } }); } }
This Main function uses the class Renderer , i.e.
class Renderer extends GLCanvas implements GLEventListener, KeyListener ,MouseListener ,MouseMotionListener {...}
And that class holds the first level of the game .
As you can see I'm also using JFrame and JOGL 1.0 .
My question is : how can I reset the JFrame after I'm done with the 1st level ? Obviously I can't use System.exit(0); , since it would quit the entire program .
What I want is to move to another class that holds the 2nd level .
How can I do that without exiting with System.exit(0); ?
Thanks
推荐答案
By using remove(), you can effectively stop the panel. Then just create a new one JFrame and add() it. Consider making the JFrame creation its own function so you don't have to keep rewriting it if do you this.
其他推荐答案
well you can use frame.dispose() then create the same JFrame with the next level...
I suggest you to restructure your code...your Main class should contain only the main method, and from where you should start the game, which would be located in another class containing the JFrame and Thread...