简体   繁体   中英

dispose JFrame inside a Thread

I have a class which extends JFrame and an inner class that extends Thread. I want the thread to run until it takes the order to dispose the Frame and open another JFrame from another class. I tries this.dispose(); and call the outer.dispose(); but non of them worked.

Here is a sample of the code:

public class outer extends JFrame{
//some code here
     public class thread extends Thread{
          // some code here
          if(something){
              //I want to dispose this frame here
}}}

Thank you in advance

A class inside a class still needs to be initialized as an object. So i suggest you make a constructor for both of the classes where the outer constructor creates an instance of thread, and thread has a constructor which takes an outer as an argument so you would include this line in the outer constructor like this: thread t = new thread(this); the overall code would be something like this:

public class outer extends JFrame{

    //Constructor which makes a instance of thread and starts it
    public outer(){
        super();
        thread t = new thread(this);
        t.start();
    }

    //some code here
    public class thread extends Thread{

        //A variable to save the parent outer
        private outer out;

        public thread(outer out){
            //Sets the parent outer
            this.out = out;
        }

        //Your run lope for testing the condition
        public void run() {
            if(condition){
                out.dispose();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM