смотрел мёртвым взглядом на мой код и никуда не попал.программа компилируется нормально, но всякий раз, когда Play.java запускает 'actionPerformed', я получаю исключение нулевого указателя из потока 'AWT-EventQueue-0', или простым языком, когда я нажимаю кнопку, он бросает NPE.Вот код (если вы хотите запустить его на своем компьютере, вам может понадобиться пропустить некоторые строки при компиляции).
Любая помощь высоко ценится заранее
Play.java: -
public class Play implements Runnable, ActionListener {
private MenuFrame menuF;
private HighScoresFrame hSF;
private GameFrame gameF;
public static void main(String[] args) {
Play program = new Play();
SwingUtilities.invokeLater(program);
}
public void run() {
menuF = new MenuFrame();
hSF = new HighScoresFrame();
gameF = new GameFrame();
}
void playGame() {
System.out.print("running the game!");
}
public void actionPerformed (ActionEvent e) {
if (e.getActionCommand() == "buttonMenu") {
menuF.setVisible(true);
hSF.setVisible(false);
}
if (e.getActionCommand() == "buttonPlayGame") {
gameF.setVisible(true);
menuF.setVisible(false);
playGame();
}
if (e.getActionCommand() == "buttonHighScores") {
hSF.setVisible(true);
menuF.setVisible(false);
}
}
}
GameFrame.java: -
public class GameFrame extends JFrame {
private static final int WINDOW_WIDTH = 1024;
private static final int WINDOW_HEIGHT = 576;
private JFrame game;
public GameFrame() {
game = new JFrame();
game.setPreferredSize(new Dimension(WINDOW_WIDTH, (WINDOW_HEIGHT + 20)));
game.setResizable(false);
game.setDefaultCloseOperation(game.EXIT_ON_CLOSE);
game.setTitle("COMS11300 Crisis - in game");
game.pack();
game.setLocationRelativeTo(null);
game.setVisible(true);
}
}
HighScoresFrame.java: -
public class HighScoresFrame extends JFrame {
private static final int WINDOW_WIDTH = 1024;
private static final int WINDOW_HEIGHT = 576;
private int highscores[] = new int[5];
private JFrame hS;
public HighScoresFrame() {
hS = new JFrame();
hS.setPreferredSize(new Dimension(WINDOW_WIDTH, (WINDOW_HEIGHT + 20)));
hS.getContentPane().add(highscoresLayeredPane());
hS.setResizable(false);
hS.setDefaultCloseOperation(hS.EXIT_ON_CLOSE);
hS.setTitle("COMS11300 Crisis - High Scores");
hS.pack();
hS.setLocationRelativeTo(null);
hS.setVisible(false);
}
JLayeredPane highscoresLayeredPane() {
JLayeredPane lPane = new JLayeredPane();
lPane.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
lPane.add(highscoresBackgroundDisplay(), new Integer(0), 0);
lPane.add(highscoresButtonsDisplay(), new Integer(1), 0);
lPane.add(highscoresDisplay(), new Integer(2), 0);
return lPane;
}
ImagePanel highscoresBackgroundDisplay() {
URL u = this.getClass().getResource("images/background_highscores.png");
ImageIcon imgIcon = new ImageIcon(u);
ImagePanel backgroundPanel = new ImagePanel(imgIcon.getImage());
backgroundPanel.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
backgroundPanel.setOpaque(true);
return backgroundPanel;
}
JPanel highscoresButtonsDisplay() {
JPanel buttonPanel = new JPanel();
JButton menu = new JButton();
URL uMenu = this.getClass().getResource("images/button_play.png");
ImageIcon imgMenuIcon = new ImageIcon(uMenu);
menu.setIcon(imgMenuIcon);
menu.setBorder(null);
menu.setActionCommand("buttonMenu");
menu.addActionListener(new Play());
buttonPanel.setBounds(730, 355, 280, 200);
buttonPanel.setOpaque(false);
buttonPanel.add(menu);
return buttonPanel;
}
String retrieveHighScores() {
String highscoresString = new String();
for (int i = 0; i < 5; i++) {
highscoresString += highscores[i];
highscoresString += "\n";
}
return highscoresString;
}
JPanel highscoresDisplay() {
JPanel scoresPanel = new JPanel();
JLabel scoresLabel = new JLabel(retrieveHighScores(), JLabel.LEFT);
scoresPanel.setBounds(100, 100, 200, 200);
scoresPanel.setOpaque(false);
scoresPanel.add(scoresLabel);
return scoresPanel;
}
}
MenuFrame.java: -
public class MenuFrame extends JFrame {
private static final int WINDOW_WIDTH = 1024;
private static final int WINDOW_HEIGHT = 576;
private JFrame menu;
public MenuFrame() {
menu = new JFrame();
menu.setPreferredSize(new Dimension(WINDOW_WIDTH, (WINDOW_HEIGHT + 20)));
menu.getContentPane().add(menuLayeredPane());
menu.setResizable(false);
menu.setDefaultCloseOperation(menu.EXIT_ON_CLOSE);
menu.setTitle("COMS11300 Crisis - Main Menu");
menu.pack();
menu.setLocationRelativeTo(null);
menu.setVisible(true);
}
JLayeredPane menuLayeredPane() {
JLayeredPane lPane = new JLayeredPane();
lPane.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
lPane.add(menuBackgroundDisplay(), new Integer(0), 0);
lPane.add(menuButtonsDisplay(), new Integer(1), 0);
return lPane;
}
ImagePanel menuBackgroundDisplay() {
URL u = this.getClass().getResource("images/background_menu.png");
ImageIcon imgIcon = new ImageIcon(u);
ImagePanel backgroundPanel = new ImagePanel(imgIcon.getImage());
backgroundPanel.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
backgroundPanel.setOpaque(true);
return backgroundPanel;
}
JPanel menuButtonsDisplay() {
JPanel buttonPanel = new JPanel();
JButton play = new JButton();
JButton highScores = new JButton();
URL uPlay = this.getClass().getResource("images/button_play.png");
URL uHighScore = this.getClass().getResource("images/button_highscore.png");
ImageIcon imgPlayIcon = new ImageIcon(uPlay);
ImageIcon imgHighScoreIcon = new ImageIcon(uHighScore);
play.setIcon(imgPlayIcon);
play.setBorder(null);
play.setActionCommand("buttonPlayGame");
play.addActionListener(new Play());
highScores.setIcon(imgHighScoreIcon);
highScores.setBorder(null);
highScores.setActionCommand("buttonHighScores");
highScores.addActionListener(new Play());
buttonPanel.setBounds(730, 355, 280, 200);
buttonPanel.setOpaque(false);
buttonPanel.add(play);
buttonPanel.add(highScores);
return buttonPanel;
}
}