0

単一フレーム内の独自のパネルに対してそれぞれ機能する 2 つのスレッドを作成しようとしていました。つまり、単一フレーム内の同じサイズの 2 つの異なるパネル内で同時に画像を変更しようとしていたということです。

独自の画像を実行している 2 つの異なるフレームに対して実行しましたが、独自のパネルの単一のフレームでこれを実行したいと考えています。

私のコーディングは:

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);***** を使用して同様のコードを実行してくださいそれだけ

4

1 に答える 1