0

my name is Gab.

I'm making a program that will make vanish one page for an other by clicking a button. The thing is, I can't pass the appropriate datas from my two classes. It may sounds weird like this, but here's my code:

My first class (class1):

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;

public class class1 {
    public static void main(String[] args) {

        JPanel panel = new JPanel();
        JFrame frame = new JFrame();

        GridBagLayout gridBag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        contenu.setLayout(gridBag);
        boolean start = false;

        JLabel label1 = new JLabel(
                "<html><p><span style = 'font-size: 18px; font-color: blue'>The Number's Genius </span>(version 1.4)</p></html>");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.gridwidth = 4;
        c.ipadx = 500;
        panel.add(label1, c);

        JLabel xxx = new JLabel("");
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipadx = 550;
        panel.add(xxx, c);

        JButton start = new JButton("<html><p><b>Start</b></p></html>");
        c.gridx = 3;
        c.gridy = 4;
        c.gridwidth = 1;
        c.ipadx = 1;
        c.anchor = GridBagConstraints.LINE_END;
        panel.add(start, c);

        JLabel msg1 = new JLabel(
                "<html><p style = 'background-color: white; font-size: 10px'>Click on Start to begin.</p></html>");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 4;
        panel.add(msg1, c);

        JLabel msg2 = new JLabel(
                "<html><p style = 'background-color: white; font-size: 10px'>Ok, let's begin.</p><p style = 'background-color: white; font-size: 10px'>First question: Is your number even?</html>");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 3;
        c.gridwidth = 4;
        panel.add(msg1, c);

        Moteur moteur = new Moteur();
        start.addActionListener(moteur);

        frame.setContentPane(contenu);
        frame.setSize(700, 300);
        frame.setVisible(true);
        frame.setResizable(false);
    }
}

You might notice that the second class has some text in french, but don't worry, it's not important. Here's my second class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Moteur implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        msg1.setVisible(false);
        msg2.setVisible(true);
    }
}

In fact, I want the program to make 'msg1' visibility to false and 'msg2' visibility to true, but I can't make the program work because the second class don't know what is 'msg1' and 'msg2'. Please, help me!

Regards, -Gab

2 Answers2

2

For a method to be able to use an object, it needs a reference to this object. You're creating a Moteur instance, but you don't pass any object to this Moteur instance, so it doesn't have a reference to any object except itself.

For the Moteur class to be able to call methods of msg1 and msg2, you need to pass a reference to these two objects to Moteur:

public class Moteur implements ActionListener {
    private JLabel messageToHide;
    private JLabel messageToShow;

    public Moteur(JLabel messageToHide, JLabel messageToShow) {
        this.messageToHide = messageToHide;
        this.messageToShow = messageToShow;
    }

    public void actionPerformed(ActionEvent e) {
        messageToHide.setVisible(false);
        messageToShow.setVisible(true);
    } 
}

And then, when you create the Moteur, you give them the two labels to hide and show:

Moteur moteur = new Moteur(msg1, msg2);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Use getters and setters for msg's.

Vasiliy
  • 1
  • 1
  • 2