當我重寫我的GamePanel類的paint方法,那麼,當我嘗試打開MazeGUI設計選項卡,我得到這個錯誤:的Java Swing - 顯示java.lang.NullPointerException
Internal Error
WindowBuilder encountered unexpected internal error.
This could be caused by a WindowBuilder bug or by a misconfiguration issue, conflict, partial update, etc.
Stack trace:
java.lang.NullPointerException
at maze.gui.GamePanel.paint(GamePanel.java:25)
at javax.swing.JComponent.print(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.printChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.print(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.printChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
如果我刪除在GamePanel.java上的paint override函數
g.drawImage(maze.wall, 400, 400, null);
MazeGUI的設計選項卡已經可以工作。很奇怪......
無論如何,如果我運行java應用程序,它會顯示圖像沒有任何錯誤。
這裏,我離開這兩個類java文件: GamePanel.java:
package maze.gui;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class GamePanel extends JPanel {
private int comprimento_sprites;
private MazeGUI maze;
/**
* Create the panel.
*/
public GamePanel(MazeGUI m) {
comprimento_sprites = 40;
maze = m;
setFocusable(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(maze.wall, 400, 400, null);
}
}
MazeGUI.java:
package maze.gui;
import java.awt.EventQueue;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import maze.logic.Jogo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class MazeGUI {
private JFrame frmMazeGame;
//IMAGES
public BufferedImage wall;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MazeGUI window = new MazeGUI();
window.frmMazeGame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MazeGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//faz load das imagens do jogo
loadImages();
frmMazeGame = new JFrame();
frmMazeGame.setTitle("Maze Game");
frmMazeGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GamePanel tabuleiro = new GamePanel(this);
frmMazeGame.getContentPane().add(tabuleiro);
}
private void loadImages(){
try {
wall = ImageIO.read(new File("images/bloco.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
啊,讓我補充這個: - 我認爲所有這個問題都與l有關MazeGUI的oadImages功能,但我認爲我做的一切都好。已經失去了這麼多小時試圖解決這個問題。
- 請記住:沒有錯誤,當我運行應用程序(它甚至可以顯示在應用程序中的圖像),但我得到上面,當我嘗試打開設計選項卡中的錯誤:/
你能清理代碼,只顯示重要的部分,只是堆棧跟蹤的第一部分?它變得非常混亂和複雜。 – Haedrian 2013-04-10 20:41:37
完成! :)我認爲你現在可以更好地理解代碼...... – 2013-04-10 20:45:23
運行程序時,設計選項卡和執行上下文的執行上下文是不同的。這意味着設計視圖無法找到圖像。也。不要重寫paint,而是使用paintComponent,並確保你也調用了super.paintComponent。而不是將核心邏輯放在框架內,而是使用面板。這使得你的程序更加靈活 – MadProgrammer 2013-04-10 20:48:48