显示来自文件选择器的图像

use*_*295 2 java swing image

我在显示从我创建的文件选择器获得的图像时遇到问题。你能给我建议吗?图像被创建为缓冲图像。

这是我的代码:

public void actionPerformed(ActionEvent e)
{
    if (e.getSource().getClass().getName().contains("JMenuItem")) 
    {
        if (e.paramString().contains("Load")) {
            JFileChooser fc = new JFileChooser();
            fc.setCurrentDirectory(new File("."));
            int retVal = fc.showOpenDialog(null);
            if (retVal == 0) 
            {
                File file = fc.getSelectedFile();
                try {
                image = ImageIO.read(file);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是显示代码:

public void paint(Graphics g){  
    super.paintComponents(g);
    g.drawImage(getIconImage(), 0, 0, control);
    g.drawImage( image, 0, 0,null);
    repaint();
}
Run Code Online (Sandbox Code Playgroud)

mKo*_*bel 5

为什么与(S)的2D图形用于显示图片,放困扰图像/ ImageIcon的的JLabel,例如约的JFileChooser +图像+ paintCompoent()

public void paint(Graphics g){// paintComponent not paint 
   super.paintComponents(g);  // paintComponent not paintComponents
.....
Run Code Online (Sandbox Code Playgroud)

可能是 SwingJComponent

public void paintComponent(Graphics g){  
   super.paintComponent(g);
......
Run Code Online (Sandbox Code Playgroud)