首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JAVA ECLIPSE:未加载耦合到标签的图像

JAVA ECLIPSE:未加载耦合到标签的图像
EN

Stack Overflow用户
提问于 2019-03-01 02:32:35
回答 2查看 36关注 0票数 0

有人知道为什么我的图像没有加载吗?我尝试了很多东西,但到目前为止什么都没有,首先,文件夹"IMGFiles“已经很像源文件夹了。

    package Main;

import javax.swing.*;

public class Menu extends JFrame {

    public Menu()
    {
        ImageIcon imagem = new ImageIcon(Menu.class.getResource("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png"));
        JLabel logo = new JLabel();
        logo.setIcon(imagem);
    }

    public static void main(String[] args) {
        //new Menu();
        JFrame janela = new JFrame();
        janela.setSize(816, 419);
        janela.setUndecorated(true);
        janela.setVisible(true);
        janela.setLocationRelativeTo(null);
    }

}
EN

回答 2

Stack Overflow用户

发布于 2019-03-01 03:38:35

我假设您正在使用UNIX (对于您正在使用的路径的形状)。以下是为您提供的一种方法:

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import com.apple.eawt.Application;

public class Menu extends JFrame {

    public Menu(){ }

    public static void main(String[] args) {

        Application.getApplication().setDockIconImage(new ImageIcon("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png").getImage());

        //new Menu();
        JFrame janela = new JFrame();
        janela.setSize(816, 419);
        janela.setUndecorated(true);
        janela.setVisible(true);
        janela.setLocationRelativeTo(null);
    }

}

输出:

票数 0
EN

Stack Overflow用户

发布于 2019-03-01 03:43:45

这是因为您准备了JLabel inside Menu构造函数,而没有初始化Menu类的任何实例。此外,不要将JLabel添加到框架(内容窗格)中。

检查此示例:

public class Menu extends JFrame {

    public Menu()
    {
        ImageIcon imagem = new ImageIcon(Menu.class.getResource("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png"));
        JLabel logo = new JLabel();
        logo.setIcon(imagem);
        setSize(816, 419);
        setUndecorated(true);
        setLocationRelativeTo(null);
        getContentPane().add(logo); //Add the label to the content pane
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->{
            new Menu().setVisible(true);
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54932113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档