簡體   English   中英

在類java之間進行通信

[英]Communicating between classes java

我有一個名為myJPanel1的類,我在其中創建了Student類的實例。 我還在myJPanel1中創建了一個按鈕,顯示有關這個新學生的信息。 當我單擊該按鈕時,我想在單獨的JPanel更改按鈕的文本,該類在名為myJPanel2類中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);
    }
}

你需要的是建立某種形式的合同,允許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());
        }
    }  
}

發生這種情況是因為你沒有任何對按鈕的引用,你創建它們並將它們分配給一個臨時引用,當構造函數完成時它的作業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如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM