所以我无法弄清楚为什么我无法将图像加载到这个JPanel中....代码有点长,但我对它进行了评论,所以推断它的发展应该相当简单。有人可以帮忙吗?我尝试删除背景颜色,但似乎没有做太多(除了删除背景颜色)
我设法修复了在没有添加到的类中调用drawScene方法的错误,现在它将显示加载图像时出错...我不完全确定为什么虽然。这是我正在使用的图像,如果有人想尝试自己运行程序。我把它添加到我的Window.java文件下面的包中。
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.");
}
}
}
}