Umstrukturierung von MainFrame, sodass beim druecken eines Buttons parameter an die Funktion uebergeben werden welche das naechste Panel stellt (showPanelExtra)

This commit is contained in:
Kaver 2024-11-26 16:42:00 +01:00
parent 1fc1abee3a
commit 5cabd5325a
3 changed files with 38 additions and 8 deletions

View File

@ -4,7 +4,7 @@ import javax.swing.JFrame;
import javax.swing.*;
public class JoinGame extends JPanel {
JLabel spielBeitretenLabel= new JLabel("Spiel beitreten");
JLabel spielBeitretenLabel;
JLabel ipLabel = new JLabel("IP-Adresse");
JLabel portLabel = new JLabel("Port");
@ -15,12 +15,18 @@ public class JoinGame extends JPanel {
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
public JoinGame(MainFrame frame) {
public JoinGame(MainFrame frame,int g) {
setLayout(null);
buildPanel(frame);
buildPanel(frame,g);
}
private void buildPanel(MainFrame frame,int g) {
if(g==1){
spielBeitretenLabel= new JLabel("Spiel beitreten");
}else{
spielBeitretenLabel= new JLabel("Spiel erstellen");
}
private void buildPanel(MainFrame frame) {
spielBeitretenLabel.setBounds(20,20,700, 100);
losButton.setBounds(320, 225, 100, 50);

View File

@ -5,6 +5,7 @@ public class MainFrame extends JFrame {
private CardLayout cardLayout;
private JPanel mainPanel;
int localMult;
public MainFrame() {
setTitle("Studium Versenken");
@ -27,13 +28,13 @@ public class MainFrame extends JFrame {
startLocalGame localGame = new startLocalGame(this);
startMultiplayerGame multiplayerGame = new startMultiplayerGame(this);
coinToss coinToss = new coinToss(this);
JoinGame joinGame = new JoinGame(this);
//JoinGame joinGame = new JoinGame(this,localMult);
mainPanel.add(mainMenuView, "MainMenu");
mainPanel.add(localGame, "LocalGame");
mainPanel.add(multiplayerGame, "MultiplayerGame");
mainPanel.add(coinToss, "coinToss");
mainPanel.add(joinGame, "JoinGame");
//mainPanel.add(joinGame, "JoinGame");
// Hauptpanel in JFrame hinzufügen
add(mainPanel);
@ -46,6 +47,27 @@ public class MainFrame extends JFrame {
cardLayout.show(mainPanel, panelName);
}
public void showPanelExtra(String panelName,int num) {
this.localMult = num;
if (!isPanelPresent(panelName)) {
JoinGame joinGame = new JoinGame(this, localMult);
mainPanel.add(joinGame, panelName); // Dynamically add the panel
mainPanel.revalidate(); // Refresh the layout
mainPanel.repaint();
}
cardLayout.show(mainPanel, panelName); // Show the panel
}
private boolean isPanelPresent(String panelName) {
for (Component component : mainPanel.getComponents()) {
if (panelName.equals(mainPanel.getClientProperty("name"))) {
return true;
}
}
return false;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainFrame frame = new MainFrame();

View File

@ -6,7 +6,8 @@ public class startMultiplayerGame extends JPanel {
// Funktionshilfen
int semesterCounter = 1; // Semester Counter Label
String PlayerNickname = "Spieler 1";
int multlocal;
String test;
// Grafiken
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
@ -109,7 +110,8 @@ public class startMultiplayerGame extends JPanel {
// ActionListener für den "Back" Button, um zum vorherigen Panel zurückzukehren
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
joinGameButton.addActionListener(e -> frame.showPanel("JoinGame"));
joinGameButton.addActionListener(e -> frame.showPanelExtra("JoinGame",1));
createGameButton.addActionListener(e -> frame.showPanelExtra("JoinGame",0));
}
// TOGGLE METHODEN