本文介绍了为什么我的图片没有加载到JFrame上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 伙计为什么我的图像没有加载到我的JFrame上? BTW我在我的mac上使用eclipse。 代码董事会: package ourgame; import java.awt。*; import java.awt.event。*; import javax.swing。*; public class Board extends JPanel implements ActionListener { Dude p; public 图片img; 定时器时间; public Board(){p = new Dude() ; addKeyListener( new AL()); setFocusable(true); ImageIcon i = new ImageIcon( C :/character.png); img = i.getImage(); time = new 计时器( 5 , this ); time.start(); } public void actionPerformed(ActionEvent e){ p.move(); repaint(); } public void paint(Graphics g){ super .paint(g); Graphics2D g2d =(Graphics2D)g; g2d.drawImage(img, 0 , 0 ,null); g2d.drawImage(p.getImage(),p.getX(),p.getY(),null); } private class AL extends KeyAdapter { public void keyReleased(KeyEvent e) { p.keyReleased(e); } public void keyPressed(KeyEvent e){ p.keyPressed(e); } } } Dude代码: package ourgame; import java.awt。*; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; public class Dude { int x,dx,y; 图像仍然; public Dude(){ ImageIcon i = new ImageIcon ( C:/background.jpg); still = i.getImage(); x = 10 ; y = 172 ; } public void move(){x = x + dx; } public int getX(){ return x; } public int getY(){ return y; } public 图片getImage(){ return } public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) dx = - 1 ; if (key == KeyEvent.VK_RIGHT) dx = 1 ; } public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) dx = 0 ; if (key == KeyEvent.VK_RIGHT) dx = 0 ; } } 框架代码: package ourgame; import javax.swing。*; public class 框架{ public Frame(){ JFrame frame = new JFrame(); frame.add( new Board()); frame.setTitle( 2-D Test Game); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize( 700 , 365 ); frame.setVisible(true); frame.setLocationRelativeTo(null); } public static void main( String [] args){ new Frame(); } } 提前致谢!解决方案 你正在运行你的您的Mac上的应用程序,但您的图像路径是一个Windows路径。 ImageIcon i = new ImageIcon(C:/character.png); 。 Mac上的路径应为/User/xxx/your_projects/background.jpg。这是一条绝对的道路。您可以通过键盘快捷键command + i检查图像路径。如果项目中有图像文件,也可以使用项目的相对路径。 ImageIcon i = new ImageIcon(background.jpg); Guys why is my image not loading on my JFrame??BTW I am using eclipse on my mac.Code for Board :package ourgame;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Board extends JPanel implements ActionListener { Dude p; public Image img; Timer time; public Board() { p = new Dude(); addKeyListener(new AL()); setFocusable(true); ImageIcon i = new ImageIcon("C:/character.png"); img = i.getImage(); time = new Timer(5, this); time.start(); } public void actionPerformed(ActionEvent e) { p.move(); repaint(); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(img, 0, 0, null); g2d.drawImage(p.getImage(), p.getX(), p.getY(), null); } private class AL extends KeyAdapter { public void keyReleased(KeyEvent e) { p.keyReleased(e); } public void keyPressed(KeyEvent e) { p.keyPressed(e); } }}Code for Dude :package ourgame;import java.awt.*;import java.awt.event.KeyEvent;import javax.swing.ImageIcon;public class Dude { int x, dx, y; Image still; public Dude() { ImageIcon i = new ImageIcon("C:/background.jpg"); still = i.getImage(); x = 10; y = 172; } public void move() { x = x + dx; } public int getX() { return x; } public int getY() { return y; } public Image getImage() { return still; } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) dx = -1; if (key == KeyEvent.VK_RIGHT) dx = 1; } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) dx = 0; if (key == KeyEvent.VK_RIGHT) dx = 0; }}Code for Frame:package ourgame;import javax.swing.*;public class Frame { public Frame(){ JFrame frame = new JFrame(); frame.add(new Board()); frame.setTitle("2-D Test Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(700,365); frame.setVisible(true); frame.setLocationRelativeTo(null); } public static void main(String[] args){ new Frame(); }}Thanks in advance! 解决方案 you are running your app on your mac, but your image path is a windows path. ImageIcon i = new ImageIcon("C:/character.png");. The path on Mac should be "/User/xxx/your_projects/background.jpg". This is an absolute path. You can check the image path by keyboard shortcut "command + i". And you can also use relative path to your project if you have the image file in your project. ImageIcon i = new ImageIcon("background.jpg"); 这篇关于为什么我的图片没有加载到JFrame上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 12:15