2
votes

I have to make a game for school and I've been having some trouble with switching JPanels with a click on the JButton. I want to use a CardLayout, but I'm new to Java which makes it very hard. My goal is to have all my Panels in different classes, like class 'Panel 1', class 'Panel 2' etc. (instead of creating my JPanels in my main (JFrame) class, so my code is easier to read. Is it possible to put your CardLayout container in the class which contains my JFrame? And also, where do I put that darn ActionPerformed? Here is my code, hope you guys can help me!

MAIN (JFrame) CLASS

    package invers;

    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class InversMain extends JFrame implements ActionListener

     {

public CardLayout cardlayout;
public Container contentPane = this.getContentPane();
public InversMain()
{


JFrame frame = new JFrame();
frame.setLayout(cardlayout);
frame.setSize(1366,768);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Invers");
frame.setResizable(true);
frame.setVisible(true);

contentPane.setPreferredSize(new Dimension(600, 400));
    contentPane.add(new InversMainPaneel(), "Panel 1");
    contentPane.add(new InstellingenPaneel(), "Panel 2");

    settingsButton.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        cardlayout.show(contentPane, "Panel 1");
    }
});}


public static void main ( String [] args)
{
    new InversMain();
}
}

Note that the settingsButton is my button from the PANEL 1 class. Because it isn't created in my main class, it gives an error. I want to refer to my settingsButton from PANEL 1 class, from within my main class. Is this possible?

PANEL 1, PANEL CONTAINING MY BUTTONS, THIS IS MY MAIN MENU PAGE

    package invers;

    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.JButton;
    import javax.swing.JPanel;

    public class InversMainPaneel extends JPanel

    {

private JButton nieuwSpelKnop = new JButton("Nieuw spel");
private JButton laadSpelKnop = new JButton("Laad Spel");
private JButton settingsButton = new JButton("Settings");
private JButton handleidingKnop = new JButton("Handleiding");

public InversMainPaneel()
{

    this.setLayout(null);

    nieuwSpelKnop.setSize(300,40);
    nieuwSpelKnop.setFont(new Font("Arial", Font.BOLD, 25));
    nieuwSpelKnop.setLocation(520,250);
    nieuwSpelKnop.setVisible(true);


    laadSpelKnop.setSize(300,40);
    laadSpelKnop.setFont(new Font("Arial", Font.BOLD, 25));
    laadSpelKnop.setLocation(520,350);
    laadSpelKnop.setVisible(true);

    settingsButton.setSize(300,40);
    settingsButton.setFont(new Font("Arial", Font.BOLD, 25));
    settingsButton.setLocation(520,450);
    settingsButton.setVisible(true);

    handleidingKnop.setSize(300,40);
    handleidingKnop.setFont(new Font("Arial", Font.BOLD, 25));
    handleidingKnop.setLocation(520,550);
    handleidingKnop.setVisible(true);


    this.add(nieuwSpelKnop);
    this.add(laadSpelKnop);
    this.add(settingsButton);
    this.add(handleidingKnop);

    this.setBackground(new Color(178,143,79));
}
    }
    }

PANEL 2, FOR TESTING IF THE CARDLAYOUT WORKED

    package invers;
    import java.awt.Color;
    import javax.swing.JPanel;

    public class InstellingenPaneel extends JPanel
    {
public InstellingenPaneel()
{
    this.setBackground(new Color(178,143,79));
}
    }
2
Format better your code please, it´s hardly readableRubén
Here's a working example, or reference.trashgod

2 Answers

0
votes

Have you tried setting the JFrame contentpane using the setContentPane method?

Because I can see you declaring JFrame and a contentpane object, but not setting it, or setting it with your panels.

0
votes

Note that the settingsButton is my button from the PANEL 1 class. Because it isn't created in my main class, it gives an error.

I'm guessing that the error you're getting is telling you that cardlayout is referenced in an inner class and thus must be made final. To fix this problem, simply insert the final keyword on your creation of cardlayout.

Second - yes, it is perfectly acceptable (and in line with best practices) to define your panel types in separate classes and then create instances of those classes to place in your JFrame.

Third, it appears that you have "that darned actionPerformed" in the right place (i.e. as a method in your ActionListener inner-class), but you should add the @Override annotation to it. Do you know what I mean by that?

Finally, if you want to refer to the settingsButton from another class, you have several options. I would recommend declaring settingsButton as an instance variable of your JFrame class, and passing a reference to the JFrame to the constructor of the InversMainPanel class:

public InversMainPanel(InversMain im) {
    ...
    im.settingsButton.  //do something with the settings button.

having created an InversMainPanel from the InversMain class like this:

   InversMainPanel imp = new InversMainPanel(this)

with this referring to the InversMain instance from which the call is being made.

Let me know if I can explain any of this further.

PS: Check out this tutorial on Java naming conventions