From ae886a415e304d42f1d44bb255c6540c4c5d987c Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Mon, 27 Dec 2021 17:18:22 +0100 Subject: [PATCH] gameexplorer: added action listeners for navigation between game and menu --- .../de/tims/gameexplorer/GameExplorer.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index 3c15004..cde21e3 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -1,6 +1,7 @@ package de.tims.gameexplorer; import java.awt.*; +import java.awt.event.*; import javax.swing.*; public class GameExplorer { @@ -36,7 +37,7 @@ public class GameExplorer { buildNavigationPanel(); buildGamePanels(); - frame.add(gamePanel); + frame.add(explorerPanel); frame.setMinimumSize(minSize); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -51,12 +52,16 @@ public class GameExplorer { fleetstormBtn = new JButton("Schiffe versenken"); fleetstormBtn.setPreferredSize(btnSize); + fleetstormBtn.addActionListener(new GameAction()); fourwinsBtn = new JButton("Vier gewinnt"); fourwinsBtn.setPreferredSize(btnSize); + fourwinsBtn.addActionListener(new GameAction()); tictactoeBtn = new JButton("TicTacToe"); tictactoeBtn.setPreferredSize(btnSize); + tictactoeBtn.addActionListener(new GameAction()); leaderboardBtn = new JButton("Leaderboard"); leaderboardBtn.setPreferredSize(btnSize); + leaderboardBtn.addActionListener(new GameAction()); border1 = new JPanel(); border1.setOpaque(false); @@ -120,6 +125,7 @@ public class GameExplorer { navigationPanel.setLayout(new GridBagLayout()); backBtn = new JButton("< Zurück"); + backBtn.addActionListener(new BackAction()); chosenGame = new JLabel(); @@ -153,6 +159,26 @@ public class GameExplorer { gamePanel.add(navigationPanel, BorderLayout.PAGE_START); } + private class GameAction implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + frame.remove(explorerPanel); + frame.add(gamePanel); + frame.revalidate(); + frame.repaint(); + } + } + + private class BackAction implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + frame.remove(gamePanel); + frame.add(explorerPanel); + frame.revalidate(); + frame.repaint(); + } + } + public static void main(String[] args) { new GameExplorer(); }