【发布时间】:2011-04-20 15:50:52
【问题描述】:
我正在制作一个包含三个主面板和几个子面板的游戏,我对如何“连接”这些面板及其数据感到困惑。
我有我的主类,它扩展了 JFrame 并添加了三个 JPanel。这些面板中的每一个都是 JPanel 的子类。 (例如:JPanel gameControlPanel = new GameControlPanel(),其中 GameControlPanel 是我为扩展 JPanel 而创建的一个类。)
现在,所有游戏数据(例如游戏状态,以及保存已保存玩家和已保存分数的两个数组列表)都在游戏面板中。但我需要从其他两个面板获取和设置该数据。而如何做到这一点是在逃避我。
** 所以我的问题是:我该怎么做?如何从另一个 JPanel 子类(具有相同的父 JFrame)访问一个 JPanel 子类中的数据?
如果有帮助,这是扩展的 JFrame 类的代码,我在其中添加了三个面板...:
JPanel controlButtonsPanel = new GameControlButtons();
controlButtonsPanel.setPreferredSize(new Dimension(801,60));
controlButtonsPanel.setBorder(new LineBorder(Color.white, 1));
constraints.anchor = GridBagConstraints.NORTHWEST;
constraints.weightx = 2;
constraints.weighty = 0.3;
this.add(controlButtonsPanel, constraints);
JPanel gameDataPanel = new GameDataPanel();
gameDataPanel.setPreferredSize(new Dimension(500,838));
gameDataPanel.setBorder(new LineBorder(Color.white, 2));
constraints.anchor = GridBagConstraints.NORTHEAST;
constraints.weightx = 1;
constraints.weighty = 2;
this.add(gameDataPanel, constraints);
JPanel graphicsPanel = new RoofRunnerGame("Ken");
constraints.anchor = GridBagConstraints.SOUTHWEST;
constraints.weightx = 2;
constraints.weighty = 1;
graphicsPanel.setBorder(new LineBorder(Color.white, 1));
graphicsPanel.setPreferredSize(new Dimension(800,800));
graphicsPanel.requestFocus();
this.add(graphicsPanel, constraints);
graphicsPanel 包含所有这些数据:
private ArrayList<Player> savedPlayers; // Holds saved data for player's who paused and exited game.
private ArrayList<Player> savedScores; // Holds high scores from player's who played game and died.
private ArrayList<Birds> birdList = new ArrayList<Birds>(); // Not serialized due to its randomness and unimportance to player.
private ArrayList<Clouds> cloudList = new ArrayList<Clouds>(); // Not serialized due to its randomness and unimportance to player.
private Player gamePlayer; // Player object that holds all data for a game instance.
我想从其他两个面板(gameDataPanel 的类和 gameControlButton 的类)中访问该数据。
【问题讨论】:
标签: java class subclass jframe jpanel