有人知道为什么我的图像没有加载吗?我尝试了很多东西,但到目前为止什么都没有,首先,文件夹"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);
}
}
发布于 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);
}
}
输出:
发布于 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);
});
}
}
https://stackoverflow.com/questions/54932113
复制相似问题