簡體   English   中英

刷新JLabel圖標圖像

[英]Refreshing a JLabel icon image

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

對我來說很好。 我認為你的代碼中還有其他東西你沒有分享。 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);
            }
        });
    }

}

沒有必要創建一個新標簽,它可能會阻礙事情。

更改:

            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"));

另請參見此工作示例

進一步提示

  • Java GUI可能必須在許多平台上工作,在不同的屏幕分辨率和使用不同的PLAF。 因此,它們不利於組件的精確放置。 要組織強大的GUI組件,而是使用布局管理器或它們的組合,以及布局填充和白色空間邊框。 EG上面的GUI使用GridBagLayout將圖像置於JScrollPane中心。
  • 有關布局的幫助,請提供GUI的ASCII藝術,因為它應該以最小的尺寸顯示,並且(如果可調整大小)具有額外的寬度/高度。
  • 為了更好地提供幫助,請發布SSCCE

如果你看一下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();
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM