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