How to switch JPanel with buttons on each individual panel?

52 Views Asked by At

I'm new to Swing and I'm trying to figure something out. I'm having difficulty explaining what I'm trying to do and I can't find anything online so I wrote a program demonstrating what I want to have happen. I use a CardLayout here:

import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class Test extends JFrame implements ActionListener {
  private CardLayout crd;
  private int currentCard;

  // Buttons
  private JButton twoToCardOne;
  private JButton threeToCardOne;
  private JButton toCardTwo;
  private JButton toCardThree;

  // Container
  private Container cPane;

  // Panels
  private JPanel cardOne;
  private JPanel cardTwo;
  private JPanel cardThree;

  public Test() {
    currentCard = 1;
    cPane = getContentPane();
    crd = new CardLayout();
    cPane.setLayout(crd);

    // Create buttons
    twoToCardOne = new JButton("To Card One");
    threeToCardOne = new JButton("To Card One");
    toCardTwo = new JButton("To Card Two");
    toCardThree = new JButton("To Card Three");
    twoToCardOne.addActionListener(this);
    threeToCardOne.addActionListener(this);
    toCardTwo.addActionListener(this);
    toCardThree.addActionListener(this);

    // Create Panel (Card One)
    cardOne = new JPanel();
    cardOne.add(toCardTwo);
    cardOne.add(toCardThree);

    // Create Panel (Card Two)
    cardTwo = new JPanel();
    cardTwo.add(twoToCardOne);

    // Create Panel (Card Three)
    cardThree = new JPanel();
    cardThree.add(threeToCardOne);

    // Add Panels
    cPane.add(cardOne);
    cPane.add(cardTwo);
    cPane.add(cardThree);
  }

  @Override
  public void actionPerformed(ActionEvent e){
    int target = 0;
    if (e.getSource() == toCardThree){target = 3;}
    else if (e.getSource() == toCardTwo){target = 2;}
    else {target = 1;}

    while (currentCard != target){
      crd.next(cPane);
      if (currentCard < 3){++currentCard;}
      else {currentCard = 1;}
    }
  }

  public static void main(String[] args){
    Test test = new Test();
    test.setSize(800, 500);
    test.setVisible(true);
    test.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
}

So, what I have here are buttons on various panels that all connect to the CardLayout. So, there are different buttons on the different panels that will move me panels when clicked on.

I have two questions/worries here:

  1. If I wanted to make each JPanel its own class, how would I make this work? Since the buttons will be in a different class, I would no longer be able to control the CardLayout with actionPerformed.

  2. My plan is to make this a hockey program where clicking on a button will take you to the player's profile with their stats and my plan was to have each individual player be a panel. This means that I will have A LOT of panels and buttons and I was wondering if there would be a better way to go about doing this. The only thing I've found online is to use CardLayout but this seems like it would not be a very optimal way of doing this.

Any help is much appreciated!

Note: Again, I'm very new to Swing so apologies if what I'm trying to explain is confusing or if I got something completely wrong.

0

There are 0 best solutions below