gpt4 book ai didi

java - 如何在 JPanel 对象上调用 getWidth

转载 作者:行者123 更新时间:2023-12-01 18:33:37 24 4
gpt4 key购买 nike

我需要调用getWidth()在我的 JPanel 对象“gameboard”上并将其存储在变量“width”中。这看起来很简单

int width = gameboard.getWidth();

但是我收到错误“在定义字段之前无法引用字段”。这让我相信引用了错误的对象,所以我将其更改为 int width = this.gameboard.getWidth();现在 Eclipse 显示 gameboard 指的是 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;

基于此你会遇到很多问题......

  1. 当你初始化width时和height , GameBoard将是null 。这将导致NullPointerException ,会发生这种情况,因为实例字段在构造函数执行之前就已初始化
  2. 如果您要初始化 GameBoard当它被声明时( GameBoard gameboard = new GameBoard() ),那么当 width 时和height被读取,它们将是 0 ,如gameBoard不会被添加到父容器中,也不会被布局。

这就提出了一个问题,为什么?如果您需要知道 gameboard 的宽度或高度,你应该在需要知道的时候问它。

你应该知道,随着框架大小的调整,以这种方式存储值是没有用的,如果用户调整框架大小,这些值就会变得无效......

旁注...

  • setFocusable(true);实在是没啥用,有一个JRootPane键盘和框架本身之间的内容 Pane 及其内容(可能还有玻璃 Pane ),其中任何一个都可能会窃取框架的焦点,从而使其无用......
  • addKeyListener(new MyKeyListener());由于上述原因,这是没有用的,你应该使用 key bindings相反,它克服了所有焦点问题......
  • setVisible(true);应该在初始化 UI 并添加所有初始组件之后最后完成

关于java - 如何在 JPanel 对象上调用 getWidth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com