java:按next和previouse按钮更改图片

时间:2017-08-25 12:07:58

标签: java

[代码无效100%正确。 当我点击下一步它将改变图片(它正在工作)。但是当我按下previouse按钮转到上一张图片时,它不会显示上一张图片。它将显示下一张图片,然后再显示当前的图片然后将显示预先图片。 请帮帮我 ] [1]

//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--;
    }
}

2 个答案:

答案 0 :(得分: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]);
    }
}

我甚至不知道这是否能解决你的问题;我还没有找到你问题的原因,因为代码很难读,我放弃了找到它。

答案 1 :(得分: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])));
}