2

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));
}
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

0

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

drew moore
  • 31,565
  • 17
  • 75
  • 112
  • I've made my cardlayout final, and added an override like this: @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } Still, I don't know in which class you paste the line: "public InversMainPanel(InversMain im) { ... im.settingsButton." or where to put : 'InversMainPanel imp = new InversMainPanel(this)' – PimPaddenburg Apr 23 '13 at 17:34
  • `"public InversMainPanel(InversMain im) {` is the constructor for your InversMainPanel class - you'll replace its current constructor `"public InversMainPanel() {`` with it, and from now on whenever you create a new `InversMainPanel` you'll have to pass a reference to an `InversMain` instance. `InversMainPanel imp = new InversMainPanel(this)' ` goes wherever in the `InversMain` class where you're creating an `InversMainPanel` – drew moore Apr 23 '13 at 19:33
  • pleeeease look at the tutorial on naming conventions – drew moore Apr 23 '13 at 19:34
  • Alright, in my Main Panel, I added imp.settingsButton to my button, but now it gives me the error that the field InversMainPanel.settingsButton is not visible? – PimPaddenburg Apr 24 '13 at 16:29
  • That's because you've made `settingsButton` private. Make it package-private by removing the `private` keyword - this will make it available to other classes in your package (namely your MainPanel), but still protect it from any classes outside of your package. – drew moore Apr 25 '13 at 16:46
  • Okay, I deleted the 'private' keyword, but now I get the error: "the blank final field cardlayout may not have been initialized." Thank for all your help btw. – PimPaddenburg May 07 '13 at 12:25
0

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.

Rubén
  • 524
  • 5
  • 22