更新jlabel中包含的映像-问题

polkgigr  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(293)

应用程序的一部分,我目前有麻烦去工作是能够滚动和显示一个图像列表,一次一个。我从用户那里得到一个目录,后台处理该目录中的所有文件,然后加载一个仅包含jpeg和png的数组。接下来,我想用第一个图像更新jlabel,并提供上一个和下一个按钮来依次滚动和显示每个图像。当我试图显示第二个图像时,它没有得到更新。。。到目前为止我得到的是:

public class CreateGallery
{
    private JLabel swingImage;

我用于更新图像的方法:

protected void updateImage(String name) 
{
    BufferedImage image = null;
    Image scaledImage = null;
    JLabel tempImage;

    try
    {
        image = ImageIO.read(new File(name));
    } catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // getScaledImage returns an Image that's been resized proportionally to my thumbnail constraints
    scaledImage = getScaledImage(image, THUMB_SIZE_X, THUMB_SIZE_Y);
    tempImage = new JLabel(new ImageIcon(scaledImage));
    swingImage = tempImage;
}

然后在我的createandshowgui方法中把swingimage放在。。。

private void createAndShowGUI() 
{
    //Create and set up the window.
    final JFrame frame = new JFrame();

    // Miscellaneous code in here - removed for brevity

    //  Create the Image Thumbnail swingImage and start up with a default image
    swingImage = new JLabel();
    String rootPath = new java.io.File("").getAbsolutePath();
    updateImage(rootPath + "/images/default.jpg");

    // Miscellaneous code in here - removed for brevity

    rightPane.add(swingImage, BorderLayout.PAGE_START);
    frame.add(rightPane, BorderLayout.LINE_END);
public static void main(String[] args) 
{
    SwingUtilities.invokeLater(new Runnable() 
    {
        public void run() 
        {
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            new CreateGalleryXML().createAndShowGUI();
        }
    });
}

如果到目前为止,第一个映像是my default.jpg,一旦我得到目录并标识该目录中的第一个映像,当我尝试更新swingimage时,它就会失败。现在,我尝试了swingimage.setvisible()和swingimage.revalidate()来强制它重新加载。我猜根本原因是我的tempimage=new jlabel。但我不知道如何将我的bufferedimage或image转换为jlabel,以便只更新swingimage。

scyqe7ek

scyqe7ek1#

而不是创建一个 New InstanceJLabel 对于每个 Image ,只需使用 JLabel 更改图像。
小样本程序:

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[] icons = {UIManager.getIcon("OptionPane.informationIcon"),
                            UIManager.getIcon("OptionPane.errorIcon"),
                            UIManager.getIcon("OptionPane.warningIcon")};
    private ImageIcon pictures1, pictures2, pictures3, pictures4;
    private ActionListener action = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {                       
            i++;
            System.out.println(i);

            if(i == 1)
            {
                pictures1 = new ImageIcon("image/caIcon.png");
                images.setIcon(icons[i - 1]);
                System.out.println("picture 1 should be displayed here");
            }
            if(i == 2)
            {
                pictures2 = new ImageIcon("image/Keyboard.png");
                images.setIcon(icons[i - 1]);   
                System.out.println("picture 2 should be displayed here");
            }
            if(i == 3)
            {
                pictures3 = new ImageIcon("image/ukIcon.png");
                images.setIcon(icons[i - 1]);
                System.out.println("picture 3 should be displayed here");  
            }
            if(i == 4)
            {
                pictures4 = new ImageIcon("image/Mouse.png");
                images.setIcon(icons[0]);   
                System.out.println("picture 4 should be displayed here");  
            }
            if(i == 5)
            {
                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();
            }
        });
    }
}

既然您是用imageio完成的,下面是一个与使用imageio的jlabel相关的好例子
与您的案件有关的信息,以及正在发生的事情:
在你的 createAndShowGUI() 方法初始化 JLabel (swingimage),你把它加到你的 JPanel 因此间接地 JFrame .
但现在你的内心 updateImage() 方法,您正在初始化一个新的 JLabel ,现在它驻留在另一个内存位置 tempImage = new JLabel(new ImageIcon(scaledImage)); 在这之后你指着你的 swingImage(JLabel) 指向这个新创建的 JLabel ,但这是新创建的 JLabel 从未添加到 JPanel ,在任何时候。因此,即使您尝试,它也不会可见 revalidate()/repaint()/setVisible(...) . 因此,要么您更改 updateImage(...) 方法:

protected void updateImage(String name) 
{
    BufferedImage image = null;
    Image scaledImage = null;
    JLabel tempImage;

    try
    {
        image = ImageIO.read(new File(name));
    } 
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // getScaledImage returns an Image that's been resized 
    // proportionally to my thumbnail constraints
    scaledImage = getScaledImage(image, THUMB_SIZE_X, THUMB_SIZE_Y);
    tempImage = new JLabel(new ImageIcon(scaledImage));
    rightPane.remove(swingImage);
    swingImage = tempImage;
    rightPane.add(swingImage, BorderLayout.PAGE_START);
    rightPane.revalidate();
    rightPane.repaint(); // required sometimes
}

或者使用 JLabel.setIcon(...) 如前所述:-)
更新了答案
在这里看看 New JLabel 放在原来的位置上,

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[] icons = {UIManager.getIcon("OptionPane.informationIcon"),
                            UIManager.getIcon("OptionPane.errorIcon"),
                            UIManager.getIcon("OptionPane.warningIcon")};
    private ActionListener action = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {                       
            i++;
            System.out.println(i);          

            if(i == 4)
            {
                timer.stop();
                System.exit(0);
            }
            remove(images);
            JLabel temp = new JLabel(icons[i - 1]);
            images = temp;
            add(images);
            revalidate();
            repaint();
        }
    };

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("SLIDE SHOW");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);    

        this.setLayout(new FlowLayout(FlowLayout.LEFT));    

        add(images);

        frame.getContentPane().add(this, BorderLayout.CENTER);

        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().createAndDisplayGUI();
            }
        });
    }
}

至于你的问题:在我尝试过的两个选择中,一个比另一个好吗? setIcon(...) 从某种意义上说,在添加/删除之后,您不必为revalidate()/repaint()之类的事情操心 JLabel . 此外,您不需要记住 JLabel 每次,你都要加上它。它保持在它的位置,你只需调用一个方法来更改图像,没有附加任何字符串,工作就完成了,没有任何麻烦。
对于第二个问题:我有点怀疑,到底是什么 Array of Records ?

相关问题