簡體   English   中英

在線程內部配置JFrame

[英]dispose JFrame inside a Thread

我有一個擴展JFrame的類和一個擴展Thread的內部類。 我希望線程運行,直到它按順序處理Frame並從另一個類打開另一個JFrame。 我嘗試this.dispose(); 並調用outer.dispose(); 但他們沒有工作。

以下是代碼示例:

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
}}}

先感謝您

類中的類仍然需要初始化為對象。 所以我建議你為外部構造函數創建一個線程實例的兩個類創建一個構造函數,而線程有一個構造函數,它將外部作為參數,所以你要在外部構造函數中包含這一行,如下所示: thread t = new thread(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();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM