我目前遇到麻煩的應用程序部分能夠滾動瀏覽並顯示圖像列表,一次一個。我從用戶那裏獲得一個目錄,並在該目錄中的所有文件後面打印,然後加載一個只包含jpegs和png的數組。接下來,我想用第一張圖片更新JLabel,並提供上一個和下一個按鈕,依次滾動並顯示每個圖像。當我嘗試顯示第二圖像,它沒有更新......這就是我這麼遠:更新包含在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;
}
在放swingImage我createAndShowGUI方法
則...
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();
}
});
}
如果你已經得到了這一步,第一個圖像是我default.j pg,一旦我得到目錄並確定該目錄中的第一個映像,那麼當我嘗試更新swingImage時,它就會失敗。現在,我試圖swingImage.setVisible()和swingImage.revalidate()來嘗試強制它重新加載。我猜這是我的tempImage =新的JLabel,這是根本原因。但我不知道如何將我的BufferedImage或Image轉換爲JLabel以更新swingImage。
我已經添加了一些關於爲什麼你的'setVisible()/ revalidate()'調用從未在這種情況下工作的信息。希望這會讓這件事變得多一點,至於爲什麼你會得到這個意想不到的行爲:-) – 2012-04-07 06:51:22
我之前嘗試過setVisible(false)的不同組合,而不是在swingImage = tempImage之後將其設置回true。並且在setVisible(true)之前和之後嘗試使用.revalidate()。但我開始相信這是新的JLabel(新的ImageIcon(縮小的圖像),導致這個問題......仍然看着它,並試圖找出它...... – Osh 2012-04-07 06:58:22