0
votes

I've just approached Java and I'm working on a project for my University class. I'm working on a Milionaire game but I'm stuck.

I've got a JFrame class in which I have 2 panels. The first one is made of buttons, the second one is the panel I want to change by pressing the buttons. Buttons have their own class with their constructor and the same is for the panels cause they have a different layout. I need to create a method in the button class to remove the second panel from the frame and add a third panel (described in another more JPanel class). So I technically need to acess from button class method to my JFrame class constructor. Is there a way to do it?

I've got my first Panel class and my Button class with its ClickListener method. Now I need to know how can i modify my JFrame class in my Button method to close the first Panel at click, opening in the same position another one.

Button Method

public class ClickListenerD1 implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        buttonPressed();
    }

    private void buttonPressed()
    {
        JPanel panel3 = new Domanda1();
    }
}

Main JFrame class

package nuovaPartita;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Visualizza la finestra di gioco.
 */
public class NuovaPartitaViewer extends JFrame
{
    private static final int FRAME_LUNGH = 1600;
    private static final int FRAME_ALT = 900; 

    JPanel panel1 = new NuovaPartitaComp1();
    JPanel panel2 = new Start();
    /**
     * Costruisce una finestra di gioco su cui vengono visualizzati due 
pannelli.
     */
    public NuovaPartitaViewer()
    {
        setSize(FRAME_LUNGH, FRAME_ALT);
        setTitle("CHI VUOL ESSER MILIONARIO?");
        setVisible(true);
        setLocationRelativeTo(null);
        setResizable(false);

        BorderLayout layout = new BorderLayout();
        getContentPane().setLayout(layout);
        getContentPane().setBackground(Color.BLACK);
        add(panel1, BorderLayout.WEST);
        add(panel2, BorderLayout.CENTER);
    }
}

Thanks

1
"I need to create a new JPanel with a Button click from another Panel to substitute it..." Can you please describe it a bit more? Seems unclear.progyammer
Yeah sure! I'm sorry first time using stackoverflow. I've got a JFrame class in which I have 2 panels. The first one is made of buttons, the second one is the panel I want to change by pressing the buttons. Buttons have their own class with their constructor and the same is for the panels cause they have a different layout. I need to create a method in the button class to remove the second panel from the frame and add a third panel (described in another more JPanel class). So I technically need to acess from button class method to my JFrame class constructor. Is there a way to do it?Matteo Bonetti
You can implement your listeners inside your JFrame you then have access to the jFrame and all its contentuser5156016
You can also keep the reference to your panels inside the buttons.user5156016
Maybe take a look at anonymous classes (or even lambdas with functional interfaces) to see how you can add the actionPerformed stuff to your panel classWietlol

1 Answers

1
votes

You can just set the panel corresponding to a button in the button's action listener.

enter image description here

public class NuovaPartitaViewer extends JFrame {

    public NuovaPartitaViewer() {
        JPanel p1 = new JPanel();
        p1.add(new JLabel("Panel 1"));
        JPanel p2 = new JPanel();
        p2.add(new JLabel("Panel 2"));
        JPanel p3 = new JPanel();
        p3.add(new JLabel("Panel 3"));
        JPanel p4 = new JPanel();
        p4.add(new JLabel("Panel 4"));

        JButton b1 = new JButton("Button 1");
        b1.addActionListener(e -> setPanel(p1));
        JButton b2 = new JButton("Button 2");
        b2.addActionListener(e -> setPanel(p2));
        JButton b3 = new JButton("Button 3");
        b3.addActionListener(e -> setPanel(p3));
        JButton b4 = new JButton("Button 4");
        b4.addActionListener(e -> setPanel(p4));

        JPanel buttonsPanel = new JPanel(new GridLayout(2, 2));
        buttonsPanel.add(b1);
        buttonsPanel.add(b2);
        buttonsPanel.add(b3);
        buttonsPanel.add(b4);

        BorderLayout layout = new BorderLayout();
        getContentPane().setLayout(layout);
        getContentPane().setBackground(Color.BLACK);
        add(buttonsPanel, BorderLayout.WEST);
        add(p1, BorderLayout.CENTER);

        setTitle("CHI VUOL ESSER MILIONARIO?");
        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    private void setPanel(JPanel p) {
        add(p, BorderLayout.CENTER);
        revalidate();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new NuovaPartitaViewer());
    }
}

If all your buttons are customized (extends JButton) then you can add the action listener code directly in that class. Pass the corresponding JPanel in the constructor so you can use it in the action listener.

Additionally:

  • Don't set the size of the frame, use pack() instead.
  • Call setVisible(true) as the last thing you do in the method.