简体   繁体   中英

Why this GUI app does not show me the image?

I'm trying yo write a java app with a JFrame object that must show three label objects, with text and image, my text "North" and "South" shows up on execution, but my image does not, even I put the image file into src folder.

package deitel9;

import java.awt.BorderLayout;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

import javax.swing.JFrame;


public class LabelDemo {

    public static void main(String[] args)
    {
         //crate a label with a plain text
        JLabel northLabel = new JLabel("North");

        //crate an icon from an image so we can put it on a JLabel
        ImageIcon labelIcon = new ImageIcon("maldive.jpg");

        //crate a label with an Icon instead of text
        JLabel centerLabel = new JLabel(labelIcon);

        //create another label with an Icon
        JLabel southLabel = new JLabel(labelIcon);

        //set the label to display text (as well as an icon)
        southLabel.setText("South");

        //create a frame to hold the labels
        JFrame application = new JFrame();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //add the labels to the frame; the second argument specifies
        //where on the frame to add the label

        application.add(northLabel,BorderLayout.NORTH);
        application.add(centerLabel,BorderLayout.CENTER);
        application.add(southLabel,BorderLayout.SOUTH);

        application.setSize(300,300);
        application.setVisible(true);
    }//end main


}//end class LabelDemo

Since your image stored in same package where LabelDemo is stored, try this,

ImageIcon labelIcon = new ImageIcon(LabelDemo.class.getResource("/deitel9/maldive.jpg").getFile());

or

private String getImage() {
    return getClass().getResource("/deitel9/maldive.jpg").getFile();
}

ImageIcon labelIcon = new ImageIcon(new LabelDemo().getImage());

To figure out what you did wrong in a situation like this, you can simply call

File file = new File ("maldive.jpg");
System.out.println(file.getAbsolutePath());

This will print out the absolute path it is looking at for your file, which might give you an indication about what you did wrong.

Of course if you know how to use the debugger, you don't need the second line (and technically not even the first one, but that's a bit trickier ;))

According to the documentation , the ImageIcon constructor you chose expects a file name or file path, thus the image needs to be on file system rather than on the class path.

Creates an ImageIcon from the specified file. [...] The specified String can be a file name or a file path.

Given your description, when your project layout looks like this

\---src
    \---deitel9
        LabelDemo.java
        maldive.jpg

then you should be able to retrieve the image as resource located on the class path like this:

ImageIcon labelIcon = new ImageIcon(LabelDemo.class.getResource("maldive.jpg"));

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