【问题标题】:Refreshing a JLabel icon image刷新 JLabel 图标图像
【发布时间】:2013-07-03 01:51:49
【问题描述】:

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

第一次就可以了,但是每当我去改变图像时,它仍然是我第一次设置的,所以我试过了,结果还是一样。

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

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

【问题讨论】:

  • 看到这个post
  • @JavaDevil 谢谢,ImageIO 解决方案有效。我不知道我的搜索结果中怎么没有出现。

标签: java swing jframe jlabel imageicon


【解决方案1】:

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

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

  • 避免使用null 布局(看起来您可能正在使用一种)
  • 避免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);
            }
        });
    }

}

【讨论】:

  • 我没有使用 ImageIO 读取文件,所以图像显然被缓存了。
【解决方案2】:

没有必要创建新标签,它可能会阻碍事情。

变化:

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

收件人:

        lblPlaceholder.setIcon(new ImageIcon("tempimage.png"));

另请参阅working example

更多提示

  • Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要组织强大的 GUI 组件,请改用布局管理器或它们的组合,以及用于空白空间的布局填充和边框。例如。上面的 GUI 使用 GridBagLayout 将图像居中在 JScrollPane 中。
  • 有关布局的帮助,请提供 GUI 的 ASCII 艺术,因为它应该以最小尺寸显示,并且(如果可调整大小)具有额外的宽度/高度。
  • 如需尽快获得更好的帮助,请发帖 SSCCE

【讨论】:

    【解决方案3】:

    如果您查看 ImageIcon 构造函数,您会看到它使用以下方法加载图标:image = Toolkit.getDefaultToolkit().getImage(location); 该方法的文档说:

     * Returns an image which gets pixel data from the specified URL.
     * The pixel data referenced by the specified URL must be in one
     * of the following formats: GIF, JPEG or PNG.
     * The underlying toolkit attempts to resolve multiple requests
     * with the same URL to the same returned Image.
    

    要在每次从相同的文件名加载 ImageIcon 时刷新它,您应该在 URL 的末尾添加一些随机的内容,而不更改实际的文件名。这样的事情应该可以工作:

          try {
            URL url = new URL(file.toURI().toString() 
            + "?" + System.currentTimeMillis());
            jLabel1.setIcon(new ImageIcon(url));
          } catch (MalformedURLException ex) {
            ex.printStackTrace();
          }
    

    【讨论】:

      猜你喜欢
      • 2012-05-13
      • 2013-10-12
      • 2014-02-16
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多