简体   繁体   English

如何在Java中将图像分配给JLabel?

[英]How do I assign an image to JLabel in Java?

I want to assign an png image to JLabel . 我想将png图片分配给JLabel

I tried this code but no image appears [update]: 我尝试了此代码,但没有图像出现[更新]:

class firstPokemonChoose extends JFrame{
    private static final int WIDTH = 800; //ukuran panjang window
    private static final int HEIGHT = 800; //ukuran lebar window
    JLabel intro, tryImage;

    public firstPokemonChoose(){
        setTitle("Choose Your Pokemon"); //set judul window
        setSize(WIDTH, HEIGHT); //set ukuran

        intro = new JLabel("Please choose your first Pokemon", SwingConstants.LEFT);

        java.net.URL url = getClass().getResource("torchic.png");
        ImageIcon img = new ImageIcon(url);
        tryImage.setIcon(img);

        Container pane = getContentPane();
        pane.setLayout(new GridLayout(3, 3));
        pane.add(intro);
        pane.add(tryImage);

        setVisible(true); //set windows visible
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        validate();

    }
}

在此处输入图片说明

You have to supply to the JLabel an Icon implementation (ie ImageIcon). 您必须向JLabel提供Icon实现(即ImageIcon)。 You can do it trough the setIcon method, as in your question, or through the JLabel constructor: Try this 您可以通过问题中的setIcon方法或通过JLabel构造函数来完成此操作:

tryImage.setIcon(new ImageIcon(path));

I recommend to put your images in your project because in this way, your image will be portable with the project and no path should be modify in the code. 我建议将图像放入项目中,因为这样,您的图像将随项目一起移植,并且无需在代码中修改任何路径。

If the image is in your project classpath, try this: 如果图像在您的项目类路径中,请尝试以下操作:

java.net.URL url = getClass().getResource("/Resources/torchic.png");
ImageIcon image = new ImageIcon(url);
tryImage .setIcon(image);

If not, you have to define the absolute path of the image as below: 如果没有,则必须定义图像的绝对路径,如下所示:

tryImage.setIcon(new ImageIcon("path\\torchic.png"));

Update: 更新:

You have 2 errors in your code: 您的代码中有2个错误:

  • The first that you should give the complete path of the image "/Resources/torchic.png" and not only torchic.png 首先,您应该提供图像的完整路径"/Resources/torchic.png" ,而不仅仅是torchic.png

  • The second is you are trying to set a icon to the not initialize tryImage JLabel 第二个是您正在尝试将图标设置为未初始化的tryImage JLabel

    Edit your code as below: 如下编辑代码:

     class firstPokemonChoose extends JFrame{ private static final int WIDTH = 800; //ukuran panjang window private static final int HEIGHT = 800; //ukuran lebar window JLabel intro, tryImage; public firstPokemonChoose(){ setTitle("Choose Your Pokemon"); //set judul window setSize(WIDTH, HEIGHT); //set ukuran intro = new JLabel("Please choose your first Pokemon", SwingConstants.LEFT); tryImage = new JLabel(); // initialize the tryImage label java.net.URL url = getClass().getResource("/Resources/torchic.png"); ImageIcon img = new ImageIcon(url); tryImage.setIcon(img); Container pane = getContentPane(); pane.setLayout(new GridLayout(3, 3)); pane.add(intro); pane.add(tryImage); setVisible(true); //set windows visible setDefaultCloseOperation(EXIT_ON_CLOSE); validate(); } public static void main(String [] args){ firstPokemonChoose firstPokemonChooseInstance = new firstPokemonChoose(); } } 
  • 声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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