【问题标题】:dispose JFrame inside a Thread在线程内处理 JFrame
【发布时间】:2017-05-13 22:18:41
【问题描述】:

我有一个扩展 JFrame 的类和一个扩展 Thread 的内部类。 我希望线程一直运行,直到它需要处理 Frame 并从另一个类打开另一个 JFrame。 我尝试 this.dispose();并调用 external.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
}}}

提前谢谢你

【问题讨论】:

    标签: java jframe


    【解决方案1】:

    类中的类仍然需要初始化为对象。所以我建议你为两个类创建一个构造函数,其中外部构造函数创建一个线程实例,并且线程有一个构造函数,它接受一个外部作为参数,所以你可以像这样在外部构造函数中包含这一行: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();
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      相关资源
      最近更新 更多