2017-08-25 55 views
-1

[該代碼無法100%正確工作。 當我點擊下一步它會改變圖片(它工作)。但是當我按previouse按鈕去previouse圖片它不會顯示previouse圖片。它會顯示下一張圖片,然後當前圖片再顯示prevoiuse圖片。 幫我請 ] [1]java:按next和previouse按鈕更改圖片

//next picture 
int count=0; 
String[] imagenames={"black.png","blue.png","gray.png","green.png","orange.png","purple.png","red.png","yellow.png"}; 
private void btnnextActionPerformed(java.awt.event.ActionEvent evt) {           

    ImageIcon[] imagelist= new ImageIcon[8]; 
    for(int i = 0; i < imagelist.length;i++){ 
     imagelist[i]= new ImageIcon(getClass().getResource("/images/"+ imagenames[i])); 
     if(count<0) count = 0; 
     if(count>=0 && count < imagenames.length){ 
      jLabel1.setIcon(imagelist[count]); 
      count++; 
     } 
    } 

}

//previous picture 
//i use the same array imagenames 
private void btnbackActionPerformed(java.awt.event.ActionEvent evt) {           
    ImageIcon[] imagelist= new ImageIcon[8]; 
    for(int i = 0; i < imagelist.length;i++){ 
    imagelist[i]= new ImageIcon(getClass().getResource("/images/"+ imagenames[i])); 
    } 
    if(count >= imagenames.length)count= imagenames.length-2; 

    if(count>=0&& count < imagenames.length){ 
     jLabel1.setIcon(imagelist[count]); 
     count--; 
    } 
} 

回答

0

它是一種很難理解你的代碼,代碼的重複並不能真正幫助。每次單擊按鈕時,都會初始化ImageIcon陣列,這也是一種浪費。你可以這樣做的方法,如下所示:

class MyComponent { 
    ImageIcon[] imagelist = new ImageIcon[8]; 
    { 
     imagenames[] = { //... }; 
     for (int i = 0; i < imagelist.length; i++) { 
      imagelist[i] = new ImageIcon(getClass().getResource("/images/"+ imagenames[i])); 
     } 
    } // now you can reuse the Icons every time 

    int currentImageIndex = 0; // better name, no? 
    private void btnnextActionPerformed(java.awt.event.ActionEvent evt) { 
     currentImageIndex = Math.max(currentImageIndex+1, imagelist.length-1); 
     updateIcon(); // reuse in both button handler methods 
    } 
    private void btnbackActionPerformed(java.awt.event.ActionEvent evt) { 
     currentImageIndex = Math.min(currentImageIndex-1, 0); 
     updateIcon(); 
    } 
    private void updateIcon() { 
     jLabel1.setIcon(imagelist[currentImageIndex]); 
    } 
} 

我甚至不知道是否可以解決您的問題;我沒有找到你的問題的原因,因爲代碼是如此難以閱讀,我放棄找到它。

0

我認爲這能夠以更簡單的方式來完成,像這樣:

對於「下一張圖片」按鈕:

private void btnnextActionPerformed(java.awt.event.ActionEvent evt) { 
    // check if we are out of the upper limit 
    if (count >= imagenames.length-1) { 
     count = imagenames.length-2; 
    } 

    // check if we are out of the lower limit 
    if (count < 0) { 
     count = 0; 
    } 

    // switch to next image 
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/images/"+ imagenames[++count]))); 
} 

對於「一張圖片」按鈕:

private void btnbackActionPerformed(java.awt.event.ActionEvent evt) { 
    // check if we are out of the upper limit 
    if (count > imagenames.length-1) { 
     count = imagenames.length-1; 
    } 

    // check if we are out of the lower limit 
    if (count =< 0) { 
     count = 1; 
    } 

    // switch to prev image 
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/images/"+ imagenames[--count]))); 
}