이 코드는 간단한 애니메이션으로 JFrame을 인스턴스화 한 것으로 여기에서 버튼을 클릭하고 스레드를 사용하여 계속 실행합니다. JFrame을 인스턴스화하는 버튼이있는 UI를 만들었습니다. 문제는 버튼을 다시 클릭하면 다른 JFrame이 인스턴스화되고 두 번째 애니메이션이 제대로 작동하지 않습니다. 5 개의 JFrames를 인스턴스화하더라도 애니메이션을 시작하는 모든 버튼은 첫 번째 JFrame에서만 작동합니다. 내가하고 싶은 것은 인스턴스만큼이나 원하는대로 할 수 있으며, 모두가 별도로 작동합니다. 다음은 애니메이션이있는 클래스의 코드입니다.Java에서 스레드를 사용하여 애니메이션이있는 둘 이상의 JFrame을 인스턴스화하는 방법은 무엇입니까?
public class duduleo extends JFrame implements ActionListener, Runnable{
JButton imagens;
int x=1;
static ImageIcon icon[] = new ImageIcon[11];
static JLabel l;
boolean anima=false;
public static void main(String args[]){
JFrame janela = new duduleo();
janela.show();
}
duduleo(){
icon[0]= new ImageIcon("duken1.jpg");
icon[1]= new ImageIcon("duken2.jpg");
icon[2]= new ImageIcon("duken3.jpg");
icon[3]= new ImageIcon("duken4.jpg");
icon[4]= new ImageIcon("duken5.jpg");
icon[5]= new ImageIcon("duken6.jpg");
icon[6]= new ImageIcon("duken7.jpg");
icon[7]= new ImageIcon("duken8.jpg");
icon[8]= new ImageIcon("duken9.jpg");
icon[9]= new ImageIcon("duken10.jpg");
icon[10]= new ImageIcon("duken11.jpg");
setSize(100,200);
setLocation(200,150);
getContentPane().setLayout(new GridLayout(2,1));
imagens = new JButton(icon[0]);
l= new JLabel(icon[0]);
imagens.addActionListener(this);
getContentPane().add(l);
getContentPane().add(imagens);
}
public void run(){
while(anima){
for(int i=0;i<icon.length;i++){
l.setIcon(icon[i]);
try{
Thread.sleep(100);
}catch(Exception e){}
}}}
public void actionPerformed(ActionEvent e){
anima=!anima;
Thread t;
t = new Thread(this);
t.start();
}}
감사합니다.
EDT에서 while/for 루프를 실행하지 마십시오. [동시성의 동시성] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) 및 [Event Dispatch Thread] (http://docs.oracle.com/javase/tutorial/)를 참조하십시오. uiswing/concurrency/dispatch.html) – asgs
아니요, 두 번째 버튼을 클릭하면 다른 JFrame이 인스턴스화되지 않습니다. –