Killing all processes, force everything to stop

十年热恋 提交于 2019-12-13 04:25:14

问题


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.

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.


回答1:


I remember having that issue in my own game..

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

i.e

MyPanel panel=new MyPanel();

...

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

frame.remove(panel);

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

where panel would be:

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();
    }

}

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
    }

}

now all you would do is:

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();



回答2:


Try this:

myFrame.getContentPane().remove(myPanel);
            myFrame.validate();

Make sure your music and other components are within the panel so they are removed as well.



来源:https://stackoverflow.com/questions/15285875/killing-all-processes-force-everything-to-stop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!