我试图在一个框架中创建两个线程,每个线程为其自己的面板工作,我的意思是我试图在一个框架中同时更改图像,但在一个框架中的两个相同大小的不同面板中。
我已经为运行自己的图像的两个不同框架完成了此操作,但我希望在其自己的面板中的单个框架中发生这种情况。
我的编码是:
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);***** 执行类似的代码只要