杀死所有进程,迫使一切停止 [英] Killing all processes, force everything to stop

查看:157
本文介绍了杀死所有进程,迫使一切停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于JPanel运行的游戏,其中有很多其他东西,它们都有自己的独立计时器等.看来,当我尝试从面板中卸下面板以将其替换为另一个JPanel时,它实际上拒绝结束其自身的所有进程.因此,即使我能够通过删除它并将其设置为null从面板的屏幕上删除它,它的进程仍然在后台运行,即音乐和周围的东西飞舞.

I have a game that runs off of JPanel which has on it many other things which have their own independent timers and such. It seems that when I try to remove the panel from my frame to replace it with another JPanel it refuses to actually end all of its own processes. So even if I am able to remove it from the screen of the panel by removing it and setting it null, its processes are still going off in the background, IE the music and the stuff flying around.

我需要知道的是一些有关如何完全杀死此JPanel并完全终止其寿命的解决方案.

What i need to know is some solution as to how to completely kill this JPanel and terminate its life to its entirety.

似乎没有多少人遇到过这个问题.

Seems not many people have run into this problem.

推荐答案

我记得在我自己的游戏中遇到了这个问题.

I remember having that issue in my own game..

只需创建一些自定义方法,即destroy(),它将停止所有计时器的游戏循环音乐等.

Simply create some custom method i.e destroy() which will stop all timers gameloops music etc.

MyPanel panel=new MyPanel();

...

panel.destory();//stop music, timers etc

frame.remove(panel);

//refresh frame to show changes
frame.revalidate(); 
frame.repaint();

其中面板为:

class MyPanel extends JPanel {

    private Timer t1,t2...;

    //this method will terminate the game i.e timers gameloop music etc
    void destroy() {
       t1.stop();
       t2.stop();
    }

}

或者,您可以通过每次检查面板是否可见以及是否应停止执行,使Swing Timers成为观察者.尽管现在这当然会导致您创建一个计时器,该计时器仅在面板可见后才会启动其他计时器:

Alternatively you could make your Swing Timers observers of sorts by making it check each time whether the panel is visible and if not it should stop executing. This would though now of course cause you to create a timer which will only start the others once the panel becomes visible:

class MyPanel extends JPanel {

    private Timer t1,t2,startingTimer;

    MyPanel() {
       t1=new Timer(60,new AbstractAction() {
           @Override
           public void actionPerformed(ActionEvent ae) {
               if(!MyPanel.this.isVisible()) {//if the panel is not visible
                   ((Timer)(ae.getSource())).stop();
               }
           }
       });
       startingTimer=new Timer(100,new AbstractAction() {
           @Override
           public void actionPerformed(ActionEvent ae) {
               if(MyPanel.this.isVisible()) {//if the panel is visible
                   t1.start();//start the timers
                   t2.start();
                   ((Timer)(ae.getSource())).stop();//dont forget we must stop this timer now

               }
           }
       });
       startingTimer.start();//start the timer which will check when panel becomes visible and start the others as necessary
    }

}

现在您要做的是:

frame.remove(panel);//JPanel timers should also see panel is no more visible and timer will stop

//refresh frame to show changes 
frame.revalidate(); 
frame.repaint();

这篇关于杀死所有进程,迫使一切停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆