2012-07-06 40 views
0

我点击一个按钮并在“选择文件对话框”中选择了图像文件后,我试图显示图像时遇到问题。使用JLabel显示选择的图像文件时出现的问题

最初,我设法在JLabel中显示所选图像,但后来我创建了一个单独的ActionListener,我认为从那时开始出现错误。无论我选择哪种图像,JLabel都不会显示它。

我调试它,并确保该文件选择不将图像传递到ImageIconJLabel确实从ImageIcon获得的价值,但它甚至revalidate()repaint()后不显示图像。

在这里,我附上了我的代码,供您参考!

(我修剪的代码,一个干净的外观,所以可能有一些支架没有留下有用)

package com.xxx.LoyalCardManager; 

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.sql.SQLException; 
import java.util.ArrayList; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JSeparator; 
import javax.swing.JTextField; 
import javax.swing.filechooser.FileFilter; 

public class LoyalCardManagerMain implements ActionListener{ 

private JFrame frame; 
private DatabaseHandler db = new DatabaseHandler(); 


private JLabel labelPic; 

private JButton buttonPic; 

private File picFile = new File(""); 
private BufferedImage image; 


/** 
* Launch the application. 
* @throws SQLException 
* @throws ClassNotFoundException 
*/ 
public static void main(String[] args) throws SQLException, ClassNotFoundException { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       LoyalCardManagerMain window = new LoyalCardManagerMain(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 


} 



} 

/** 
* Create the application. 
*/ 
public LoyalCardManagerMain() { 

    // Database initialisation 
    initDatabase(); 

    // Draw GUI 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 619, 487); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    buttonPic = new JButton("Click to Choose Pic"); 
    buttonPic.setBounds(415, 252, 166, 29); 
    frame.getContentPane().add(buttonPic); 
    buttonPic.setEnabled(false); 
    buttonPic.setActionCommand("ChoosePic"); 
    buttonPic.addActionListener(this); 

    labelPic = new JLabel(); 
    labelPic.setBounds(415, 30, 167, 210); 
    frame.getContentPane().add(labelPic); 




} 



public void actionPerformed(ActionEvent event) { 
    String command = event.getActionCommand(); 

    if (command.equals("ChoosePic")) { 
     //TODO Label now cannot display images. 
     JFileChooser chooser = new JFileChooser(); 
     chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
     chooser.setAcceptAllFileFilterUsed(false); 
     chooser.setFileFilter(new FileFilter() { 
      public boolean accept (File f) { 
       String extension = Utils.getExtension(f); 
       if(extension != null) { 
        if (extension.equals(Utils.gif) || 
         extension.equals(Utils.jpeg) || 
         extension.equals(Utils.jpg) || 
         extension.equals(Utils.png) || 
         extension.equals(Utils.tif) || 
         extension.equals(Utils.tiff)) { 
         return true; 
        }else{ 
         return false; 
        } 
       } 
       return false; 
      } 

      public String getDescription() { 
       return "Image File (*.gif, *.jpeg, *.jpg, *.png, *.tif, *.tiff)"; 
      } 

     }); 

     int retVal = chooser.showOpenDialog(frame); 
     if (retVal == JFileChooser.APPROVE_OPTION) { 
      picFile = chooser.getSelectedFile(); 
      try { 
       image = ImageIO.read(picFile); 
      } catch (IOException e) { 

       e.printStackTrace(); 
      } 

      // Calculate the pic's ratio and do re-scale 

      double ratio = (double) labelPic.getWidth()/(double) labelPic.getHeight(); 
      // Do image scale, scaledW is the new Width, and LabelPic.getHeight is the new Height. 
      int scaledW = (int) (image.getHeight() * ratio); 
      image = new BufferedImage(scaledW, labelPic.getHeight(), BufferedImage.SCALE_FAST); 
      ImageIcon icon = new ImageIcon(image); 

      labelPic.setVisible(true); 
      labelPic.setIcon(icon); 
      labelPic.revalidate(); 
      labelPic.repaint(); 

     } 


    } 
} 
} 

我也参考其他类似的问题:

image loading using a JFileChooser into a JFrame

Image won't display in JLabel

Updating an image contained in a JLabel - problems

外部网站:JFIleChooser opening image to JLabel

以及Java教程文档 How to Use Buttons, Check Boxes, and Radio Buttons

但我仍然无法弄清楚,为什么不的JLabel显示选择的图像。

感谢您的善良帮助队友!

+0

如果有可能尝试创建[SSCCE](http://sscce.org/),它将重现您的问题(例如,删除数据库使用情况) – Pshemo 2012-07-06 12:37:02

+0

@Pshemo好的没问题,我开始摆脱那些不相关的部分。 – dumbfingers 2012-07-06 12:38:41

回答

1

好吧,我终于想通了,什么是错的代码:

如果我打算用BufferedImage来调整(对不起,我的问题我错误理解方法scaleresize),我需要使用drawImage方法来“重绘”图像。否则图像将不会显示。

我做了修改这里:

double ratio = (double) labelPic.getWidth()/(double) labelPic.getHeight(); 
     // Do image scale, scaledW is the new Width, and LabelPic.getHeight is the new Height. 
     int scaledW = (int) (image.getHeight() * ratio); 
     image = new BufferedImage(scaledW, labelPic.getHeight(), BufferedImage.SCALE_FAST);// Edit here 
     ImageIcon icon = new ImageIcon(image); 

     labelPic.setVisible(true); 
     labelPic.setIcon(icon); 
     labelPic.revalidate(); 
     labelPic.repaint(); 

从“编辑在这里”标记,我使用下面的代码:

BufferedImage imageTemp = new BufferedImage(resizedW, resizedH, BufferedImage.TYPE_INT_RGB); 
      imageTemp.getGraphics().drawImage(image,0,0, scaledW, scaledH, null); 
      image = imageTemp; 

而且还有第一之间的差异传递价值imageTemp然后传递给image并直接将值传递给image。如果我将new BufferedImage直接传递给image,它将显示纯黑色,而不是您选择的图像。

0

尝试以此来显示图像:

 JfileChooser getImage = new JFileChooser(); 
     .......... 

     ImageIcon imagePath= new ImageIcon(getImage.getPath()); 

     JLabel imageLabel= new JLabel() { 

      @Override 
      public void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.drawImage(imagePath.getImage(), 0, 0, width, height, null); 
      } 
     }; 
     imageLabel.setLocation(10, 40); 
     imageLabel.setBorder(viewAnimalPanelBorder); 
     imageLabel.setSize(200, newHeight); 
     panel.add(imageLabel); 

让我知道如果你需要更多的援助。

此外,尝试显示图片而不使用JFileChooser,也许硬编码测试的路径。

+0

感谢您的帮助,但仍无法显示。 – dumbfingers 2012-07-06 13:08:50

+0

如果硬编码或选择图像,绕过jfilechooser,图像是否显示? – Theron084 2012-07-06 13:10:59

+0

如果我注释掉与缩放相关的方法,它的工作原理是完美无瑕的...... – dumbfingers 2012-07-06 16:41:42

相关问题