Java JPanel不会加载图像
所以我真的不明白为什么我不能将一个图像加载到这个JPanel中……代码有点长,但我已经对它进行了注释,所以推断出发生了什么应该是相当简单的。有人能帮忙吗?我尝试过删除背景色,但似乎没有什么效果(除了删除背景色) 编辑 我设法修复了drawScene方法在未添加到的类中被调用的错误,现在它将显示加载图像时出错……但我不完全确定原因。如果有人想自己运行这个程序,我会使用这个图像。我把它添加到我的包中,就在Window.java文件下面Java JPanel不会加载图像,java,image,swing,jpanel,loading,Java,Image,Swing,Jpanel,Loading,所以我真的不明白为什么我不能将一个图像加载到这个JPanel中……代码有点长,但我已经对它进行了注释,所以推断出发生了什么应该是相当简单的。有人能帮忙吗?我尝试过删除背景色,但似乎没有什么效果(除了删除背景色) 编辑 我设法修复了drawScene方法在未添加到的类中被调用的错误,现在它将显示加载图像时出错……但我不完全确定原因。如果有人想自己运行这个程序,我会使用这个图像。我把它添加到我的包中,就在Window.java文件下面 import java.awt.BorderLayout; im
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
///
///WINDOW CLASS BEGINS
///
public class Window extends JPanel{
//Instance Variables
Tools tool = new Tools();
GameScreen game = new GameScreen();
Scene scene = new Scene();
//All other variable initialization things
private static final long serialVersionUID = 1L;
public Window(){
//Sets the layout to the class
this.setLayout(new BorderLayout());
//adding things to the class
this.add(game,BorderLayout.CENTER);
//Sets background Colors to the panels
this.game.setBackground(Color.black);
}
public static void main(String args[]){
JFrame frame = new JFrame();
Window window = new Window();
frame.setSize(new Dimension(800,600));
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setJMenuBar(Tools.bar);
frame.add(window);
frame.pack();
frame.setLocationRelativeTo(null);
}
public void paintComponent(Graphics g){
}
///
// TOOLBAR CLASS BEGINS
////
public static class Tools extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
public static JMenuBar bar = new JMenuBar();
JMenu file = new JMenu("file");
JMenuItem exit = new JMenuItem("Exit");
@SuppressWarnings("static-access")
public Tools(){
//Set layouts
this.panel.setLayout(new BorderLayout());
//Add to the bar
this.bar.add(file,BorderLayout.WEST);
//add things to the inside of the bar
this.file.add(this.exit);
this.panel.add(this.bar);
//add panels
this.add(panel);
//action Commands
this.exit.setActionCommand("exit");
//add action Listeners
this.exit.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "exit"){
System.out.println("Closed");
System.exit(0);
}
}
}
/////
//GAMESCREEN CLASS BEGINS
////
public class GameScreen extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
//Instance Variables
Scene scene = new Scene();
//JPanel variables
JPanel screen = new JPanel(new BorderLayout());
public GameScreen(){
//Sets the class layout
//Adding panels
this.add(screen);
//Setting the color
this.screen.setBackground(new Color(0,0,0));
//Adding things to the panels
this.screen.add(scene);
}
@Override
public void actionPerformed(ActionEvent e) {
}
public void paintComponent(Graphics g){
scene.drawScence(g, "Backgound.jepg", 0, 0, 600, 600);
}
}
public class Scene extends JPanel{
public Scene(){
}
public void drawScence(Graphics g,String location,int x,int y,int width,int height){
try{
URL url = this.getClass().getResource(location);
Image image = ImageIO.read(url);
g.drawImage(image, x, y, width, height, null);
}catch (Exception e){
System.out.println("Unable to load image.");
}
}
}
}
1)
///WINDOW课程开始了
时间到了。哦,等等,还有8行空白。。在源代码中永远不需要超过一个空白行。2) 切勿尝试在paintComponent
方法(或它调用的任何方法)中加载资源。据我所知,Scene
从未添加到任何内容中……但这不是您在窗口的paintComponent
方法中调用的实例。
类……是的,但您有两个实例。你称之为drawScreen的不是你添加的那个。编辑:程序员比我先到的。你还在用绘画的方法阅读图像。如果正确拼写“jpeg”,也可能会有所帮助。