刷新 JLabel 图标图像 [英] Refreshing a JLabel icon image

查看:24
本文介绍了刷新 JLabel 图标图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JLabel 在 JFrame 中显示图像并设置它的图标.

I'm displaying an image in a JFrame using a JLabel and setting it's icon.

第一次就可以了,但是每当我去更改图像时,它仍然是我第一次设置的,所以我尝试了这个,仍然是相同的结果.

It works the first time, but whenever I go to change the image, it remains what I set it the first time, so I've tried this and still the same result.

                contentPane.remove(lblPlaceholder);
            lblPlaceholder = null;
            lblPlaceholder = new JLabel("");
            lblPlaceholder.setBounds(10, 322, 125, 32);
            contentPane.add(lblPlaceholder);
            lblPlaceholder.setIcon(new ImageIcon("tempimage.png"));

我怎样才能让它改变它的形象?我也尝试过重新绘制 JFrame,但没有任何结果.

How can I get it to change it's image? I've also tried repainting the JFrame with no results.

推荐答案

对我来说很好.我认为您的代码中还有其他内容您没有共享.SSCCE 将有助于澄清其他问题.

Works fine for me. I think there is something else in your code you're not sharing. A SSCCE would help clarify other issues.

根据您提供的内容提出一些建议...

Some suggestions based on what you have provided...

  • 避免 null 布局(看起来你可能正在使用一个)
  • 避免setBounds
  • Avoid null layouts (looks like you might be using one)
  • Avoid setBounds

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ShowLabelImage {

    public static void main(String[] args) {
        new ShowLabelImage();
    }

    private JLabel label;

    private List<BufferedImage> images;
    private int currentPic = 0;

    public ShowLabelImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                images = new ArrayList<>(2);
                try {
                    images.add(ImageIO.read(new File("path/to/pic1")));
                    images.add(ImageIO.read(new File("path/to/pic2")));
                } catch (IOException exp) {
                    exp.printStackTrace();
                }

                label = new JLabel();
                label.setHorizontalAlignment(JLabel.CENTER);
                label.setVerticalAlignment(JLabel.CENTER);

                JButton switchPic = new JButton("Switch");
                switchPic.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        currentPic++;
                        if (currentPic >= images.size()) {
                            currentPic = 0;
                        }
                        label.setIcon(new ImageIcon(images.get(currentPic)));
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(label);
                frame.add(switchPic, BorderLayout.SOUTH);
                switchPic.doClick();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

这篇关于刷新 JLabel 图标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆