I got thow panels. The first one contains a JTextField in order to get the username of the player
public class FirstPanel extends JPanel {
public static final JTextField usernameText = new JTextField(30);
public FirstPanel() {
this.setBackground(Color.GREEN);
JLabel enterName = new JLabel("ENTER YOUR NAME");
this.add(enterName);
this.add(usernameText);
usernameText.addActionListener(e ->
{
if (isThereUsername()) ;
CardLAyout.cl.show(CardLAyout.panelCont, "2");
});
when you enter a text you are redirected to another panel where I would like to display the following message "Hello *username obtained from the first panel thanks to the JTextfield" let's play a game.
I wrote the following code in the class of my second JPanel (the ifThereUsername function is just used to say if there was text or not). However the user name is not displayed I just read the rest of the message
if (FirstPanel.isThereUsername()) {
hello = new JLabel("HELLO " + FirstPanel.getUsername() + "\n LET'S START THE GAME RIGHT NOW ! ");
this.add(hello);
} else {
hello = new JLabel("HELLO \n let's start the game !");
this.add(hello);
}
the fonction getUsername is in the first panel
public static String getUsername(){
return usernameText.getText();
}
I really struggle to connect the classes between them at first I wrote all my JPanel in the same class but it's very messy. So I use static methods