在类java之间进行通信

时间:2014-02-24 22:29:10

标签: java swing

我有一个名为myJPanel1的类,我在其中创建了类Student的实例。我还在myJPanel1创建了一个按钮,显示有关这位新学生的信息。当我单击该按钮时,我想要在名为JPanel的类中单独编写的myJPanel2中更改按钮的文本。

我的问题是我不知道如何让myJPanel1识别myJPanel2中的按钮,以便我可以修改按钮的文字。

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);
    }
}

2 个答案:

答案 0 :(得分:3)

您需要建立某种合同,允许myJPanel1更改myJPanel2的状态,而不会将任何组件的某些部分暴露给滥用。

例如,myJPanel1不应该关心myJPanel2想要对信息做什么,只关心它可以向其提供信息。

您可以建立一个模型,该模型可以更改各种属性并向感兴趣的各方提供相应的更改通知,或者您可以在myJPanel2中提供一种方法,允许myJPanel1提供您需要的信息

例如......

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);
    }
}

然后在您的actionPerformed方法中,您只需将有效引用转到myJPanel2并将其称为setText方法,例如...

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());
        }
    }  
}

答案 1 :(得分:0)

这种情况正在发生,因为你没有任何对按钮的引用,你创建它们并将它们分配给一个临时引用,当构造函数完成时它的作业j1将没有任何生存变量来保存它们

您应该将按钮移动到JPanel班级。

示例:

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);
        ....
    }
}

评论myJPanel1如果你想在整个班级使用它的按钮,也是如此。