我需要在我的JPanel对象“ gameboard”上调用getWidth()
并将其存储在变量“ width”中。看起来很简单
int width = gameboard.getWidth();
但是我得到一个错误“在定义它之前不能引用字段”。这使我相信错误的对象被引用,所以我将其更改为
int width = this.gameboard.getWidth();
现在Eclipse显示游戏板引用了GameBoard Game.gameboard
(这是我想要的JPanel对象),但是在这里我得到了Exception in thread "AWT-EventQueue-0"
是其中的一些代码,我希望它足以解释我的情况(所有代码都超过1000行,因为我的教授写道他正在让我们进行修改)。class Game extends JFrame {
/* Type your variable declarations here */
// Score of game
int score = 0;
// Get board size
// int width = this.gameboard.getWidth();
// int height = this.gameboard.getHeight();
// Game objects -- Was going to pass width and height variable to these
Sprite cat = new Sprite(new ImageIcon("cat.gif").getImage(), 267, 167);
Sprite bananas1 = new Sprite(new ImageIcon("bananas.png").getImage(), randomNumber(0, gameboard.getWidth()), randomNumber(0, 480));
Sprite bananas2 = new Sprite(new ImageIcon("bananas.png").getImage(), randomNumber(gameboard.getWidth(),640), randomNumber(0, 480));
...
/** The panel where all of the game's images are rendered */
GameBoard gameboard = null;
...
/**
* Constructor for the game.
* <p>
* <pre>
* The following tasks are performed:
* a frame is displayed on the screen
* a clock is created; the clock invoked repaint 15 times per second
* a keyboard listener listens for presses of the arrow keys & space bar
* </pre>
* <p>
*/
public Game() {
/* how large is the window? */
setSize(640,480);
/* if the end-user clicks on the x in the upper right corner, */
/* close this app */
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* allow the window to receive the focus; */
/* we want this window to see key presses */
setFocusable(true);
/* this window can receive keyboard events */
addKeyListener(new MyKeyListener());
/* make the window visible */
setVisible(true);
/* add MyPanel to the window */
setLayout(new GridLayout(1,1));
gameboard = new GameBoard();
add(gameboard);
validate();
}//Game
/**
* Panel that displays all the graphics in the game.
* <p>
* Why do I need a panel--why can't I display the graphics in the frame?
* I want to the top of the graphics area to be at y=0.
* <p>
* Offscreen buffers are used to create a rock-solid animation that does not blink.
* <p>
* Focus is given to the panel so that the panel can listen for key presses.
* <p>
* The clock invokes repaint 15 times per second.
*/
public class GameBoard extends JPanel {
/** offscreen buffers are used to create a rock-solid animation that does not blink */
protected Image offscreenImage = null;
/** offscreen buffers are used to create a rock-solid animation that does not blink */
protected Graphics offscreenGraphics = null;
/**
* Constructor for the main panel in this game;
* all of the graphics are displayed on this panel.
* <p>
* <pre>
* Focus is given to the panel so that the panel can listen for key pressed.
* NOTE: Focus determines who receives the characters that are typed on the keyboard--
* the entity that has the focus receives the characters.
* A keyboard listener is created to listen for key pressed.
* A clock is created; the clock invokes repaint 15 times per second.
* </pre>
* <p>
*/
public GameBoard() {
/* allow this panel to get the focus */
setFocusable(true);
/* give this panel the focus */
requestFocus();
/* Now that this panel has the focus, this panel can receive keyboard events */
addKeyListener(new MyKeyListener());
/* this window can receive mouse motion events */
addMouseMotionListener(new MyMouseMotionListener());
/* this window can receive mouse events */
addMouseListener(new MyMouseListener());
/* start a clock that invokes repaint 15 times per second */
new ThreadClock().start();
}//MyPanel
...
/**
* Play the game
* @param args Command line arguments
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Game();
}
});
}//main
}//Game
最佳答案
class Game extends JFrame {
/* Type your variable declarations here */
// Score of game
int score = 0;
// Get board size
int width = this.gameboard.getWidth();
int height = this.gameboard.getHeight();
/** The panel where all of the game's images are rendered */
GameBoard gameboard = null;
基于此,您将遇到许多问题...
初始化
width
和height
时,GameBoard
将为null
。这将导致NullPointerException
,这将发生,因为实例字段在执行构造函数之前已初始化如果要在声明
GameBoard
时对其进行初始化(GameBoard gameboard = new GameBoard()
),则在读取width
和height
时,它们将为0
,因为gameBoard
不会被添加到父级中容器也不会被放置。这就提出了一个问题,为什么?如果您需要知道
gameboard
的宽度或高度,则只需在需要知道时询问即可。您应该知道,在调整框架大小之后,以这种方式存储值是没有用的,如果用户调整框架的大小,则这些值将变为无效...
旁注...
setFocusable(true);
真的没用,在键盘和框架本身之间有一个JRootPane
和内容窗格,它的内容(可能还有玻璃窗格),其中任何一个都可能夺取框架的焦点,从而使此无用...addKeyListener(new MyKeyListener());
由于上述原因,这是没有用的,您应该改用key bindings,它可以解决所有关注焦点的问题...在初始化UI并添加所有初始组件之后,应最后完成
setVisible(true);