繁体   English   中英

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

[英]Why the images are not displayed automatically?

美好的一天,我是Java语言的初学者,我正在尝试控制动画。 我对动画有问题,因为该程序无法显示图像。 我创建了一个JLabels数组,然后为此设置了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;
    }    
}

我不知道该程序会自动显示所有图像,它只会显示最后一张图像

您没有正确使用线程。 调用run()方法不是如何启动线程。 同样,Thread.sleep()导致事件调度线程进入睡眠状态,因此在执行循环中的所有代码之前,GUI无法重绘自身。

一些建议:

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

  2. 不要尝试使用线程。 相反,您应该为动画使用Swing计时器。 您启动计时器,并在每次计时器触发时更改标签的图标。 阅读Swing教程中有关如何使用计时器的部分, 获得一个简单的动画示例。

查看您的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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM