2012-03-08 55 views
0

嗨大家當提交按鈕被點擊我開始增量,當它是2我希望它改變圖片,當我是4我想改變到另一個圖片,但它不這樣做,如果如果任何人有一個想法,這將是偉大的。我正在使用eclipse,程序正在編譯和運行。下面是代碼難度與行動執行方法

/** Here is the GUI of the program 
* class name SlideShowGui.java 
* @author Kiril Anastasov 
* @date 07/03/2012 
*/ 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class SlideShowGui extends JPanel implements ActionListener, Runnable 
{ 
    JLabel name, comments, images; 
    JTextField namejtf, commentsjtf, captionjtf; 
    JButton submit; 
    ImageIcon pictures; 


    SlideShowGui() 
    { 


     name = new JLabel("Name:"); 
     this.add(name); 

     namejtf = new JTextField(15); 
     this.add(namejtf); 

     comments = new JLabel("Comments:"); 
     this.add(comments); 

     commentsjtf = new JTextField(15); 
     this.add(commentsjtf); 

     submit = new JButton("Submit"); 
     this.add(submit); 
     submit.addActionListener(this); 


     pictures = new ImageIcon("galileo1.jpg"); 
     images = new JLabel(pictures); 
     this.add(images); 


     pictures = new ImageIcon("galileo2.jpg"); 
     this.add(images); 



     captionjtf = new JTextField(24); 
     this.add(captionjtf); 

    } 

    public void actionPerformed(ActionEvent ae) 
    { 
     Thread t = new Thread(this); 
     t.start(); 

     if(ae.getSource() == submit) 
     { 

      int i = 0; 
      boolean go = true; 
      while(go) 
      { 

       i++; 
       System.out.println(i); 

       try 
       { 
        Thread.sleep(2000); 
        if(i == 2) 
        { 
         pictures = new ImageIcon("galileo2.jpg"); 
         images.setIcon(pictures); 
        } 


       } 
       catch (InterruptedException ie) 
       { 
        System.out.println("thread exception"); 
       } 
//    pictures = new ImageIcon("galileo2.jpg"); 
//    images.setIcon(pictures); 




      System.out.println("test"); 
     } 


    } 



} 

    public void run() 
    { 

    } 
} 

/**The driver class of the program. Here is the JFrame 
* class name TestSlideShow.java 
* @author Kiril Anastasov 
* @date 07/03/2012 
*/ 

import java.awt.*; 
import javax.swing.*; 
public class TestSlideShow 
{ 
    public static void main(String[] args) 
    { 
     JFrame application = new JFrame(); 
     SlideShowGui panel = new SlideShowGui(); 
     application.add(panel); 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     application.setSize(300,600); 
     application.setLocation(400,100); 
     application.setVisible(true); 


    } 

} 
+0

也許我是綁定的,但什麼是'照片=新的ImageIcon( 「galileo2.jpg」);',其中放置該文件???,在Java包天? ??,在硬盤上的某個地方?或???,因爲你的圖標/ ImageIcon漂亮可能爲空:-) – mKorbel 2012-03-08 21:12:00

回答

8

從來沒有,真的在Swing從來沒有使用Thread.sleep(),也沒有從鞦韆Listener初始化,這行代碼的原因凍結或停止在JComponent重新繪製,使用Swing Timer,剩下的就是在我answer to your previous post,CLEAN_UP這代碼

編輯--->here is four ways how to do it for Swing

+0

+1 - 我誤讀了代碼:) – MByD 2012-03-08 12:13:20

+0

@Binyamin Sharet我很高興在這裏見到你:-) – mKorbel 2012-03-08 12:15:55