为什么图像不会自动显示?

时间:2014-04-19 04:23:50

标签: java

美好的一天,我是java语言的初学者,我正在尝试控制动画。我的动画有问题,因为程序不显示图像。我创建了一个JLabel数组,然后我为此设置了72个图像。我创建了一个框架,然后我添加了2个按钮,第一个带有充电电池的图像,然后是一个滑动条,不要把注意力放在滑杆上,因为我还没有使用它。我想创建2个动画,当我按下名为“cargar”的按钮时,程序必须隐藏实际图像然后在同一位置显示下一个图像,然后程序必须等待一小段时间然后它必须重复相同的循环中的动作,因此在帧中必须运行动​​画,动画必须显示电池的充电方式。当有人按下名为“descargar”的按钮时,程序必须以相反的方式显示相同的图像,以便动画必须显示电池的放电方式。观看视频以更清楚地查看问题。我无法得到程序自动显示所有图像,它只显示最后一个图像,但奇怪的是程序可以显示每个图像没有循环,但它不是自动的,我必须多次按下按钮。我需要你的帮助来解决问题,可能有人给我一个建议,也许还有一些减少代码行的提示,谢谢你的关注。

vieo的链接: http://youtu.be/w6pINh-4TGM

这是代码:

//Referring to the package
package programa;

//imported classes
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;

//begins subclass of JFrame and implements ActionListener and Runnable interfaces
public class Intento3 extends javax.swing.JFrame implements ActionListener, Runnable{
    //global variables declaration
    //creating an array of JLabels (72 JLabels)
    JLabel[] labels = { new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel(),
    new javax.swing.JLabel()};
    //creating JButtons
    JButton jButton1 = new javax.swing.JButton();
    JButton jButton2 = new javax.swing.JButton();
    //creating JSlider
    JSlider miSlider = new JSlider(JSlider.HORIZONTAL, 0, 50, 0);
    //creating a boolean variable
    boolean carga;
    //creating integer variables
    int x = 0;
    int i;

    //start the program
    public static void main(String[] arguments){
       //creating an object of the created class
       Intento3 curso = new Intento3();
       //calling the method "cuadro" of the created class
       curso.cuadro();
    }

    //start the method "cuadro"
    public void cuadro(){
         //title of the frame
         setTitle("Animacion bateria");
         //size of the frame
         setSize(350, 350);
         //statement to close the frame
         setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
         //declaration to prohibit the change of size of the frame
         setResizable(false);
         //made the frame visible
         setVisible(true);
        //set images to the array of JLabels 
        labels[0].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila1.jpg")));
        labels[1].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila2.jpg")));
        labels[2].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila3.jpg")));
        labels[3].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila4.jpg")));
        labels[4].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila5.jpg")));
        labels[5].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila6.jpg")));
        labels[6].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila7.jpg")));
        labels[7].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila8.jpg")));
        labels[8].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila9.jpg")));
        labels[9].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila10.jpg")));
        labels[10].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila11.jpg")));
        labels[11].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila12.jpg")));
        labels[12].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila13.jpg")));
        labels[13].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila14.jpg")));
        labels[14].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila15.jpg")));
        labels[15].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila16.jpg")));
        labels[16].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila17.jpg")));
        labels[17].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila18.jpg")));
        labels[18].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila19.jpg")));
        labels[19].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila20.jpg")));
        labels[20].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila21.jpg")));
        labels[21].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila22.jpg")));
        labels[22].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila23.jpg")));
        labels[23].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila24.jpg")));
        labels[24].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila25.jpg")));
        labels[25].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila26.jpg")));
        labels[26].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila27.jpg")));
        labels[27].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila28.jpg")));
        labels[28].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila29.jpg")));
        labels[29].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila30.jpg")));
        labels[30].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila31.jpg")));
        labels[31].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila32.jpg")));
        labels[32].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila33.jpg")));
        labels[33].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila34.jpg")));
        labels[34].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila35.jpg")));
        labels[35].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila36.jpg")));
        labels[36].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila37.jpg")));
        labels[37].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila38.jpg")));
        labels[38].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila39.jpg")));
        labels[39].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila40.jpg")));
        labels[40].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila41.jpg")));
        labels[41].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila42.jpg")));
        labels[42].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila43.jpg")));
        labels[43].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila44.jpg")));
        labels[44].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila45.jpg")));
        labels[45].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila46.jpg")));
        labels[46].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila47.jpg")));
        labels[47].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila48.jpg")));
        labels[48].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila49.jpg")));
        labels[49].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila50.jpg")));
        labels[50].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila51.jpg")));
        labels[51].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila52.jpg")));
        labels[52].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila53.jpg")));
        labels[53].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila54.jpg")));
        labels[54].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila55.jpg")));
        labels[55].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila56.jpg")));
        labels[56].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila57.jpg")));
        labels[57].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila58.jpg")));
        labels[58].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila59.jpg")));
        labels[59].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila60.jpg")));
        labels[60].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila61.jpg")));
        labels[61].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila62.jpg")));
        labels[62].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila63.jpg")));
        labels[63].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila64.jpg")));
        labels[64].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila65.jpg")));
        labels[65].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila66.jpg")));
        labels[66].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila67.jpg")));
        labels[67].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila68.jpg")));
        labels[68].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila69.jpg")));
        labels[69].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila70.jpg")));
        labels[70].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila71.jpg")));
        labels[71].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila72.jpg")));
        //set properties of JSlider
        miSlider.setMajorTickSpacing(10);
        miSlider.setMinorTickSpacing(1);
        //set the titles of the buttons
        jButton1.setLabel("Cargar");
        jButton2.setLabel("Descargar");
        //set a listener to the buttons
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);

            //add image "pila1" to the frame
            add(labels[0]);
            //set the exact position of the image "pila1"
            this.setLayout(null);
            Insets insets = this.getInsets();
            Dimension size = labels[1].getPreferredSize();
            labels[0].setBounds(70,50,196,102);       
            labels[0].setVisible(true);          

            //add button1 to the frame
            add(jButton1);
            //set the exact position of the button1
            this.setLayout(null);
            Insets insets1 = this.getInsets();
            Dimension size1 = jButton1.getPreferredSize();
            jButton1.setBounds(40,200,73,30);       
            jButton1.setVisible(true);         

            //add button1 to the frame
            add(jButton2);
            //set the exact position of the button2
            this.setLayout(null);
            Insets insets2 = this.getInsets();
            Dimension size2 = jButton2.getPreferredSize();
            jButton2.setBounds(200,200,94,30);       
            jButton2.setVisible(true);      

            //set properties of JSlider
            miSlider.setPaintTicks(true);
            miSlider.setPaintLabels(true);
            //add JSlider to the frame
            add(miSlider);
            //set the exact position of the JSlider
            this.setLayout(null);
            Insets insets3 = this.getInsets();
            Dimension size3 = miSlider.getPreferredSize();
            miSlider.setBounds(70,240,200,40);       
            miSlider.setVisible(true);            
    }

    //start the method "actionPerformed"
    @Override
    public void actionPerformed(ActionEvent e) {  
        //get the argument of the buttons
        Object fuente = e.getSource();
        //if the button1 is pressed...
        if(fuente == jButton1){
            //start the "b1apretado" method
            b1apretado();
            //start the execution thread
            run();             
        }
        //if the button2 is pressed...
        if(fuente == jButton2){
            //start the "b2apretado" method
            b2apretado(); 
            //start the execution thread
            run();           
        }                
    }

    //start the execution thread 
    @Override
    public void run() {

            //if boolean variable is true...
            if(carga == true){      

            while( x > 0){
            //add image to the frame    
            add(labels[x-1]);
            //set the exact position of the image
            labels[x-1].setBounds(70,50,196,102);
            //hide the last image
            labels[x].setVisible(false);    
            //set visible the image
            labels[x-1].setVisible(true);     
            //reduce by one the variable "x"
            --this.x;
            //set a delay
            try{
            Thread.sleep(100);
            } catch (InterruptedException e){} 

            }
            }

            //if boolean variable is false...
            if(carga == false){

            while( x < 71)   {
            //add image to the frame   
            add(labels[x+1]);
            //set the exact position of the image
            labels[x+1].setBounds(70,50,196,102);
            //hide the last image
            labels[x].setVisible(false); 
            //set visible the image
            labels[x+1].setVisible(true);
            //increase by one the variable "x"
            ++this.x;
            //set a delay
            try{
            Thread.sleep(100);
            } catch (InterruptedException e){} 

            }
      }         
    }

    //start the "b1apretado" method
    public void b1apretado(){
        //set the boolean variable as true
        carga = true;
    }

    //start the "b2apretado" method
    public void b2apretado(){
        //set the boolean variable as false
        carga = false;
    }    
}

2 个答案:

答案 0 :(得分:0)

  

我无法得到程序自动显示所有图像,它只显示最后一张图像

您没有正确使用线程。调用run()方法不是如何启动Thread。此外,Thread.sleep()导致事件调度线程处于休眠状态,因此在循环中的所有代码都已执行之前,GUI无法重新绘制。

一些建议:

  1. 不要保留数组或JLabel。而是保留一个图标阵列。然后,您只需创建一个在GUI上显示的标签。对于动画,您只需使用setIcon(...)方法更改标签的图标。

  2. 不要尝试使用线程。相反,你应该使用Swing Timer来制作动画。启动Timer,每次Timer触发时,都会更改标签的Icon。阅读How to Use Timers上的Swing教程中的部分,获取一个简单的动画示例。

答案 1 :(得分:0)

查看您的actionPerformed方法,我们可以看到您没有在单独的Thread中调用run方法。调用Runnable.run()将在当前线程中执行run方法。您需要创建一个Thread对象并调用start方法:

public void actionPerformed(ActionEvent e) {  
    Object fuente = e.getSource();
    if(fuente == jButton1){
        b1apretado();                     
    }
    if(fuente == jButton2){   
        b2apretado();                     
    } 
    Thread thread=new Thread(this);
    thread.start();                  
}

但是,这种方法可能会导致异常,因为您正在更新自己的线程中的GUI。更好的方法是使用SwingWorker,正如您在tutorial中所看到的那样。