【问题标题】:Why the images are not displayed automatically?为什么图像不自动显示?
【发布时间】:2014-04-19 04:23:49
【问题描述】:

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

视频链接: 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;
    }    
}

【问题讨论】:

  • 请创建一个 MCVE - MINIMAL,完整且可验证的示例。我建议了解 for 循环,以便您可以在 3 行代码而不是 72 行代码中在图标中创建标签。

标签: java


【解决方案1】:

查看您的 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 中看到的那样。

【讨论】:

  • 谢谢!它完美地创建了一个 Thread 对象,然后使用然后使用方法 start()
  • @user3550817, It works perfectly creating a Thread object,它一直有效,直到它不起作用,然后您将花费数小时尝试调试随机问题。你被告知过两次不要使用线程。
  • 查看我在答案中添加的教程链接。正如@camickr 所说,以这种方式使用 Thread 可能会遇到麻烦,Swing 有一些专门用于处理一些并发问题的多线程类。
【解决方案2】:

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

您没有正确使用线程。调用 run() 方法不是启动线程的方法。此外,Thread.sleep() 会导致 Event Dispatch Thread 休眠,因此在循环中的所有代码都已执行之前,GUI 无法重新绘制自身。

一些建议:

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

  2. 不要尝试使用线程。相反,您应该为动画使用 Swing Timer。您启动计时器,每次计时器触发时,您都会更改标签的图标。阅读 How to Use Timers 上的 Swing 教程部分,了解一个简单的动画示例。

【讨论】:

  • 谢谢,我用计时器检查一下
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2015-12-08
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 2012-07-26
相关资源
最近更新 更多