简体   繁体   中英

Communicating between classes java

I have a class called myJPanel1 , in which I've created an instance of the class Student . I've also created a button in myJPanel1 that displays info about this new student. When I click on that button, I want to change the text of a button in a separate JPanel , written in class called myJPanel2 .

My problem is that I don't know how to get the myJPanel1 to recognize the buttons in myJPanel2 so I can modify the button's text.

myJPanel1:

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

public class myJPanel1 extends JPanel implements ActionListener {

    public myJPanel1() {
        super();
        setBackground(Color.yellow);

        Student st1 = new Student("John", "Lodger", 44);
        // the whatsUp of this student has to shown in the other panel

        JButton j11 = new JButton(st1.getInfo());
        j11.addActionListener(this);
        add(j11);

    }

    public void actionPerformed(ActionEvent Event) {
        Object obj = Event.getSource();

        if (obj == j11) {
            j2.setText(st1.whatsUp());
        }
    }  
}

myJPanel2:

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

public class myJPanel2 extends JPanel {

    public myJPanel2() {
        super();
        setBackground(Color.pink);
        //setLayout(new GridLayout(3,1));
        JButton j1 = new JButton("When the user clicks on the button in the UPPER panel");
        add(j1);

        JButton j2 = new JButton("Display here whatsUp from the student in UPPER Panel");
        add(j2);

        JButton j3 = new JButton("===>>>>You CANNOT create a student here <======");
        add(j3);

        JButton j4 = new JButton("It has to be the student from the UPPER Panel");
        add(j4);
    }
}

What you need is to establish some kind of contract that allows myJPanel1 to change the state of myJPanel2 without exposing parts of either components to misuse.

For example, myJPanel1 shouldn't care what myJPanel2 wants to do with the informaiton, only that it can provide the information to it.

You could establish a model which provided the ability to change various properties and provide appropriate change notification to interested parties or your could simply provide a method in myJPanel2 which allows myJPanel1 to supply the information you need.

For example...

public class myJPanel2 extends JPanel {
    // You'll want to use these beyond the scope of the
    // constructor
    private JButton j1;
    private JButton j2;
    private JButton j3
    private JButton j4;
    public myJPanel2() {
        super();
        setBackground(Color.pink);
        //setLayout(new GridLayout(3,1));
        j1 = new JButton("When the user clicks on the button in the UPPER panel");
        add(j1);

        j2 = new JButton("Display here whatsUp from the student in UPPER Panel");
        add(j2);

        j3 = new JButton("===>>>>You CANNOT create a student here <======");
        add(j3);

        j4 = new JButton("It has to be the student from the UPPER Panel");
        add(j4);
    }

    public void setText(String text) {
        j2.setText(text);
    }
}

Then in you actionPerformed method, you would simply take the active reference to myJPanel2 and call it's setText method, for example...

public class myJPanel1 extends JPanel implements ActionListener {

    private myJPanel2 panel2;

    public myJPanel1() {
        super();
        //...
        panel2 = new MyJPanel2();
        add(panel2);
    }

    public void actionPerformed(ActionEvent Event) {
        Object obj = Event.getSource();

        if (obj == j11) {
            panel2.setText(st1.whatsUp());
        }
    }  
}

This is happening because you don't have any reference to the buttons, You create them and assign them to a temporary reference that when the constructor is finishing it's job j1 will not have any alive variable to hold them.

You should move the buttons into your JPanel class.

Example:

public class myJPanel2 extends JPanel {

    public JButton j1;

    public myJPanel2() {
        super();
        setBackground(Color.pink);
        //setLayout(new GridLayout(3,1));
        j1 = new JButton("When the user clicks on the button in the UPPER panel");
        add(j1);
        ....
    }
}

Comment: Same goes for myJPanel1 if you want to use it's buttons all over the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM