简体   繁体   中英

displaying an image from a file chooser

I am having problems displaying an image I obtain from a file chooser I created. Could you give me suggestions? The image is created as a buffered image.

Here is my code:

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();
        }
    }
}

Here is the code for display:

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

Why bothering with 2D Graphics for display picture(s), put Image/ImageIcon to the JLabel , example about JFileChooser + Image + paintCompoent() ,

public void paint(Graphics g){// paintComponent not paint 
   super.paintComponents(g);  // paintComponent not paintComponents
.....

Could be for Swing JComponent s

public void paintComponent(Graphics g){  
   super.paintComponent(g);
......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM