刷新JLabel图标图像

问题描述 投票:4回答:3

我正在使用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。

java swing jframe jlabel imageicon
3个回答
6
投票

对我来说很好。我认为你的代码中还有其他东西你没有分享。 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);
            }
        });
    }

}

6
投票

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

更改:

            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

0
投票

如果你看一下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();
      }
© www.soinside.com 2019 - 2024. All rights reserved.