【问题标题】:not redisplaying an image s不重新显示图像
【发布时间】:2012-03-09 08:03:19
【问题描述】:

大家好,我在这里有点筹码。当我运行程序并按下按钮提交时,它应该每 2 秒更改 4 张图片。但是它不会重新显示图像。如果有人可以帮我一把,那就太好了。我正在使用eclipse,程序正在编译和运行。这是代码。

/** Here is the GUI of the program
 * class name SlideShowGui.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;   

public class SlideShowGui extends JPanel  implements ActionListener, Runnable
{
    JLabel name, comments, images;
    JTextField namejtf, commentsjtf, captionjtf;
    JButton submit;
    ImageIcon pictures1, pictures2, pictures3, pictures4;
    //ImageIcon []pictures2 = {galileo1.jpg};


    SlideShowGui()
    {


        name = new JLabel("Name:");
        this.add(name);

        namejtf = new JTextField(15);
        this.add(namejtf);

        comments = new JLabel("Comments:");
        this.add(comments);

        commentsjtf = new JTextField(15);
        this.add(commentsjtf);

        submit = new JButton("Submit");
        this.add(submit);
        submit.addActionListener(this);


        pictures1 = new ImageIcon("galileo1.jpg");
        images = new JLabel(pictures1);
        this.add(images);


        pictures2 = new ImageIcon("galileo2.jpg");
        this.add(images);
        pictures3 = new ImageIcon("galileo3.jpg");
        this.add(images);
        pictures4 = new ImageIcon("galileo4.jpg");
        this.add(images);



        captionjtf = new JTextField(24);
        this.add(captionjtf);
        //pictures = new ImageIcon("galileo1.jpg");
       // images.setIcon(pictures);  
    }

    public void actionPerformed(ActionEvent ae)
    {
        Thread t = new Thread(this);
        t.start();

        if(ae.getSource() == submit)
        {

            int i = 0;
            boolean go = true;
            while(go)
            {

                i++;
                System.out.println(i);

                try 
                { 
                    Thread.sleep(2000);

                      if(i == 1)
                      {
                            pictures1 = new ImageIcon("galileo1.jpg");
                            images.setIcon(pictures1);                                                                                                      
                            System.out.println("picture 1 should be displayed here");
                      }
                      if(i == 2)
                      {
                            pictures2 = new ImageIcon("galileo2.jpg");
                            images.setIcon(pictures2);   
                            System.out.println("picture 2 should be displayed here");

                      }
                      if(i == 3)
                      {
                           pictures3 = new ImageIcon("galileo3.jpg");
                            images.setIcon(pictures3);   
                           System.out.println("picture 3 should be displayed here");  
                      }
                      if(i == 4)
                      {
                            pictures4 = new ImageIcon("galileo4.jpg");
                            images.setIcon(pictures4);   
                           System.out.println("picture 4 should be displayed here");  
                      }


                      if(i == 4)
                      {
                              i = 0;
                      }


                } 
                catch (InterruptedException ie) 
                {
                     System.out.println("thread exception");
                }

        }
    }

}

    public void run() 
    {

    }
}

/**The driver class of the program. Here is the JFrame 
 * class name TestSlideShow.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import javax.swing.*;
public class TestSlideShow 
{
    public static void main(String[] args) 
    {
        JFrame application = new JFrame();
        SlideShowGui panel = new SlideShowGui();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(300,600);
        application.setLocation(400,100);
        application.setVisible(true);


    }

}

【问题讨论】:

  • 不不,你又忘记了,昨天@mKorbel 回答了同样的问题,你甚至忘记应用他说的一件事。永远不要在 Swing 中使用Thread.sleep(...),那会冻结你的应用程序。而是使用javax.swing.Timer 来实现这种行为。你的 EDT 在哪里?
  • @Gagandeep 巴厘岛特技演员 :-)

标签: java swing imageicon


【解决方案1】:

至少在 Swing 中始终使用javax.swing.Timer,从不使用Thread.sleep(...)。在这里尝试此代码,但请务必将 path 替换为您的图像:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SlideShow extends JPanel
{
    private int i = 0;
    private Timer timer;
    private JLabel images = new JLabel();
    private Icon bg = UIManager.getIcon("OptionPane.warningIcon");
    private Icon red = UIManager.getIcon("OptionPane.errorIcon");
    private Icon blue =  UIManager.getIcon("OptionPane.informationIcon");
    private ImageIcon pictures1, pictures2, pictures3, pictures4;
    private ActionListener action = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {           

            boolean go = true;

            i++;
            System.out.println(i);

            if(i == 1)
            {
                images.setIcon(bg);                                                                                                      
                System.out.println("picture 1 should be displayed here");
            }
            if(i == 2)
            {
                images.setIcon(red);   
                System.out.println("picture 2 should be displayed here");
            }
            if(i == 3)
            {
                images.setIcon(blue);   
                System.out.println("picture 3 should be displayed here");  
            }
            if(i == 4)
            {
                images.setIcon(bg);   
                System.out.println("picture 4 should be displayed here");  
            }
            if(i == 5)
            {
                go = false;
                timer.stop();
                System.exit(0);
            }
            revalidate();
            repaint();
        }
    };

    public SlideShow()
    {
        JFrame frame = new JFrame("SLIDE SHOW");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        frame.getContentPane().add(this);

        add(images);

        frame.setSize(300, 300);
        frame.setVisible(true); 
        timer = new Timer(2000, action);    
        timer.start();  
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new SlideShow();
            }
        });
    }
}

【讨论】:

  • @Kiril : 呵呵,记住——保持微笑:-)
  • @mKorbel : 谢谢你,保持微笑 :-)
【解决方案2】:

整个if (i == ... 部分可以简化。在SlideShowGUI中声明一个静态类成员:

private final static ImageIcon[] icons = {new ImageIcon("galileo1.jpg"),
                                          new ImageIcon("galileo2.jpg"),
                                          new ImageIcon("galileo3.jpg"),
                                          new ImageIcon("galileo4.jpg")};

并将其用于替换 if 语句:

images.setIcon(icons[i-1]);
System.printf("Picture %s should be displayed%n", i-1);
if (i == 4) {
  i = 0;
}

【讨论】:

  • (我的回答是一个一般性的提示,它不应该解决重新显示图像的实际问题......)
  • 这是one of reason why Queue is there 基础知识+1
【解决方案3】:

您可以像 Andreas_D 提到的那样简化您的代码。

当您调用 Thread.sleep() 时,您当前的设计将阻塞主线程,这将冻结您的应用程序。

如果你想更新Image,你应该在run()方法中实现更新代码。

因此,如果您检测到用户按下提交 JButton,请创建并启动新的 Thread 以更新 UI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2021-12-11
    • 2021-11-28
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多