单个JFrame中的多线程

时间:2013-06-26 21:40:41

标签: java multithreading swing

我正在尝试创建两个线程,每个线程在一个帧中为自己的面板工作,我的意思是我试图在一个帧中同时更改图像,但在单帧中在两个相同大小的不同面板中。

我已经完成了两个不同的帧运行他们自己的图像,但我希望在它自己的面板中的单个帧中发生这种情况。

我的编码是:

import javax.swing.*;
import org.omg.CORBA.Object;
import java.awt.*;

class subviewer1 extends JFrame implements Runnable{

    Thread t2 = new Thread();

    JFrame f1;
    //JLabel label;
    JPanel p1;
    ImageIcon icon; //= new ImageIcon("C:\\img.png");
        JLabel label = new JLabel(icon); 

    int imgPath = 1;

    public subviewer1(){        
        super();
        t2.start();
        /*
        f1 = new JFrame("PIC VIEWER");
        f1.setLayout(null);
        f1.setSize(500,500);
        f1.setLocation(0,0);        
        p1 = new JPanel();      
        while(true)
        {
            try {
            while (true) {          
                Thread.sleep(1000);                
                p1.setSize(500,500);
                p1.setLocation(0,0);                
                switch(imgPath)
                {
                    case 1:
                        //f1.remove(p1);
                        p1.remove(label);
                        p1.setVisible(false);
                        p1.removeAll();
                        ImageIcon icon = new ImageIcon("C:\\img.png");
                        JLabel label = new JLabel(icon); 
                        p1.add(label);
                        p1.setVisible(true);
                        imgPath = 2;
                        break;
                    case 2:
                        //f1.remove(p1);
                        p1.removeAll();
                        p1.setVisible(false);
                        ImageIcon icon1 = new ImageIcon("C:\\img1.png");
                        JLabel label1 = new JLabel(icon1);
                        p1.add(label1);
                        p1.setVisible(true);
                        imgPath = 3;
                        break;
                    case 3:
                        //f1.remove(p1);
                        p1.removeAll();
                        p1.setVisible(false);
                        ImageIcon icon2 = new ImageIcon("C:\\img2.png");
                        JLabel label2 = new JLabel(icon2); 
                        p1.add(label2);
                        p1.setVisible(true);
                        imgPath = 4;
                        break;
                    case 4:
                        //f1.remove(p1);
                        p1.setVisible(false);
                        p1.removeAll();
                        ImageIcon icon3 = new ImageIcon("C:\\img3.png");
                        JLabel label3 = new JLabel(icon3);    
                        p1.add(label3);
                        p1.setVisible(true);
                        imgPath = 1;
                        break;
                }                
                f1.add(p1);
                f1.setVisible(true);
                f1.setLayout(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
            } catch (InterruptedException iex) {
                System.out.println(iex);
            }
        }
         //initComponents();         
    }

    @Override
    public void run() {     
        {           
            f1 = new JFrame("PIC VIEWER");
            //f1.setLayout(null);
            f1.setSize(500,500);
            f1.setLocation(0,0);
            p1 = new JPanel();          
            while(true)
            {
                try {
                while (true) {              
                    Thread.sleep(1000);                 
                    p1.setSize(250,500);
                    p1.setLocation(0,0);                    
                    switch(imgPath)
                    {
                        case 1:
                            //f1.remove(p1);
                            p1.remove(label);
                            p1.setVisible(false);
                            p1.removeAll();
                            ImageIcon icon = new ImageIcon("C:\\img.png");
                            JLabel label = new JLabel(icon); 
                            p1.add(label);
                            p1.setVisible(true);
                            imgPath = 2;
                            break;
                        case 2:
                            //f1.remove(p1);
                            p1.removeAll();
                            p1.setVisible(false);
                            ImageIcon icon1 = new ImageIcon("C:\\img1.png");
                            JLabel label1 = new JLabel(icon1);
                            p1.add(label1);
                            p1.setVisible(true);
                            imgPath = 3;
                            break;
                        case 3:
                            //f1.remove(p1);
                            p1.removeAll();
                            p1.setVisible(false);
                            ImageIcon icon2 = new ImageIcon("C:\\img2.png");
                            JLabel label2 = new JLabel(icon2); 
                            p1.add(label2);
                            p1.setVisible(true);
                            imgPath = 4;
                            break;
                        case 4:
                            //f1.remove(p1);
                            p1.setVisible(false);
                            p1.removeAll();
                            ImageIcon icon3 = new ImageIcon("C:\\img3.png");
                            JLabel label3 = new JLabel(icon3);    
                            p1.add(label3);
                            p1.setVisible(true);
                            imgPath = 1;
                            break;
                    }                   
                    f1.add(p1);
                    f1.setVisible(true);
                    f1.setLayout(null);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
                } catch (InterruptedException iex) {
                    System.out.println(iex);
                }
            }       
        }   
    }
}

class subviewer2 extends JFrame implements Runnable{

    Thread t1 = new Thread();   
    JFrame f1;
    //JLabel label;
    JPanel p2;  
    ImageIcon icon; //= new ImageIcon("C:\\img.png");   
        JLabel label = new JLabel(icon);    
    int imgPath = 1;

    public subviewer2(){        
        super();
        t1.start();
        /*
        f1 = new JFrame("PIC VIEWER");
        f1.setLayout(null);
        f1.setSize(500,500);
        f1.setLocation(0,0);        
        p1 = new JPanel();

        while(true)
        {
            try {
            while (true) {          
                Thread.sleep(1000);                
                p1.setSize(500,500);
                p1.setLocation(0,0);                
                switch(imgPath)
                {
                    case 1:
                        //f1.remove(p1);
                        p1.remove(label);
                        p1.setVisible(false);
                        p1.removeAll();
                        ImageIcon icon = new ImageIcon("C:\\img4.png");
                        JLabel label = new JLabel(icon); 
                        p1.add(label);
                        p1.setVisible(true);
                        imgPath = 2;
                        break;
                    case 2:
                        //f1.remove(p1);
                        p1.removeAll();
                        p1.setVisible(false);
                        ImageIcon icon1 = new ImageIcon("C:\\img5.png");
                        JLabel label1 = new JLabel(icon1);
                        p1.add(label1);
                        p1.setVisible(true);
                        imgPath = 3;
                        break;
                    case 3:
                        //f1.remove(p1);
                        p1.removeAll();
                        p1.setVisible(false);
                        ImageIcon icon2 = new ImageIcon("C:\\img6.png");
                        JLabel label2 = new JLabel(icon2); 
                        p1.add(label2);
                        p1.setVisible(true);
                        imgPath = 4;
                        break;
                    case 4:
                        //f1.remove(p1);
                        p1.setVisible(false);
                        p1.removeAll();
                        ImageIcon icon3 = new ImageIcon("C:\\img7.png");
                        JLabel label3 = new JLabel(icon3);    
                        p1.add(label3);
                        p1.setVisible(true);
                        imgPath = 1;
                        break;
                }                
                f1.add(p1);
                f1.setVisible(true);
                f1.setLayout(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            }
            } catch (InterruptedException iex) {
                System.out.println(iex);
            }
        }
         //initComponents();        
    }

    @Override
    public void run() {     
        {

            f1 = new JFrame("PIC VIEWER");
        //  f1.setLayout(null);
            f1.setSize(500,500);
            f1.setLocation(0,0);            
            p2 = new JPanel();          
            while(true)
            {
                try {
                while (true) {              
                    Thread.sleep(1000);                 
                    p2.setSize(350,500);
                    p2.setLocation(250,0);                  
                    switch(imgPath)
                    {
                        case 1:
                            //f1.remove(p1);
                            p2.remove(label);
                            p2.setVisible(false);
                            p2.removeAll();
                            ImageIcon icon = new ImageIcon("C:\\img4.png");
                            JLabel label = new JLabel(icon); 
                            p2.add(label);
                            p2.setVisible(true);
                            imgPath = 2;
                            break;
                        case 2:
                            //f1.remove(p1);
                            p2.removeAll();
                            p2.setVisible(false);
                            ImageIcon icon1 = new ImageIcon("C:\\img5.png");
                            JLabel label1 = new JLabel(icon1);
                            p2.add(label1);
                            p2.setVisible(true);
                            imgPath = 3;
                            break;
                        case 3:
                            //f1.remove(p1);
                            p2.removeAll();
                            p2.setVisible(false);
                            ImageIcon icon2 = new ImageIcon("C:\\img6.png");
                            JLabel label2 = new JLabel(icon2); 
                            p2.add(label2);
                            p2.setVisible(true);
                            imgPath = 4;
                            break;
                        case 4:
                            //f1.remove(p1);
                            p2.setVisible(false);
                            p2.removeAll();
                            ImageIcon icon3 = new ImageIcon("C:\\img7.png");
                            JLabel label3 = new JLabel(icon3);    
                            p2.add(label3);
                            p2.setVisible(true);
                            imgPath = 1;
                            break;
                    }                   
                    f1.add(p2);
                    f1.setVisible(true);
                    f1.setLayout(null);                 
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
                } catch (InterruptedException iex) {
                    System.out.println(iex);
                }           
            }

      }
     }
}

public class thdmkr extends Thread{

        Thread t1,t2;       
        subviewer1 obj1 = new subviewer1();
        subviewer2 obj2 = new subviewer2();     
        public thdmkr(){            
            //super();          
            t1 = new Thread(obj1);
            t2 = new Thread(obj2);
            t1.start();
            t2.start();         
        }

    public static void main(String args[]){     
        thdmkr obj = new thdmkr();      
    }

    public void run(){  

        }   
}

这是针对不同的框架..但我希望在单帧中完成...所以请使用***** Thread.sleep(int args)纠正我的代码或执行类似的代码; ** ***仅

1 个答案:

答案 0 :(得分:5)

Swing不是线程安全的。也就是说,您只需要在单个线程中访问,更改或创建UI元素,称为事件调度线程(AKA EDT)。

您也应该永远不会阻止EDT,使用sleepwait或阻止IO等内容

尽管有许多可能的问题解决方案,但最简单的方法是使用javax.swing.Timer,因为它在EDT的上下文中执行其滴答,但在等待时不会阻止它。

请查看Concurrency on Swing了解详情

我还建议您只使用JLabel来显示图标,而不是添加和删除它们,只需将图标设置为新图像的参考