简体   繁体   English

JPanels之间的沟通

[英]communication between JPanels

I have a JPanel class that creates a panel1 and a panel2. 我有一个JPanel类,它创建了panel1和panel2。 Panel1 and Panel2 both have one button each. Panel1和Panel2各有一个按钮。 What I'm trying to accomplish is when I click on the button in Panel1 that it updates the button in Panel2. 我想要完成的是当我点击Panel1中的按钮时,它会更新Panel2中的按钮。 I have to do this without changing anything in Panel2. 我必须这样做而不改变Panel2中的任何内容。 Basically I have to have Panel1 have a way to keep track of an instance of Panel2 and I would have to do this through the JPanel class. 基本上我必须让Panel1有一种方法来跟踪Panel2的实例,我必须通过JPanel类来做到这一点。 But I have no clue where to begin on even accomplishing this. 但我不知道在哪里开始甚至完成这个。

myJPanel.java myJPanel.java

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

public class myJPanel extends JPanel
{
public myJPanel()
{
    super();
    setBackground(Color.gray);

    setLayout(new BorderLayout());

    myJPanel1 p1 = new myJPanel1();
    add(p1,"North");
    myJPanel2 p2 = new myJPanel2();
    add(p2,"Center");



}
}

myJPanel1.java myJPanel1.java

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

public class myJPanel1 extends JPanel
{

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

    student st1 = new student("Clark","Fontaa",26);
    // the whatsUp of this student has to shown in the other panel

    JButton jl1 = new JButton(st1.getInfo());
    add(jl1);
}
}

myJPanel2.java myJPanel2.java

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

public class myJPanel1 extends JPanel implements ActionListener
{

student st1 = new student("Fred","Fonseca",44);
JButton jl1;
public myJPanel1()
{
    super();
    setBackground(Color.yellow);

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

    public void actionPerformed(ActionEvent event){
        Object obj = event.getSource();
        if (obj == jl1){
        // something here
        }
    }

Thanks for any help! 谢谢你的帮助!

Add a new field in myJPanel1 class of type Panel so you have reference of the Jpanel2, so that you could further play around with it without changing anything in Panel 2. So follow these steps: 在Panel类型的myJPanel1类中添加一个新字段,以便您可以参考Jpanel2,这样您就可以在不更改Panel 2中的任何内容的情况下进一步使用它。请按照下列步骤操作:

  1. Add new field called Panel panel2, and in constructor add Panel parameter like myJPanel1(JPanel panel) {//set local panel2 instance variable now} 添加名为Panel panel2的新字段,并在构造函数中添加Panel参数,如myJPanel1(JPanel panel) {//set local panel2 instance variable now}
  2. In your myJPanel class, change the following: myJPanel类中,更改以下内容:

     myJPanel1 p1 = new myJPanel1(); add(p1,"North"); myJPanel2 p2 = new myJPanel2(); add(p2,"Center"); 

    To

     myJPanel2 p2 = new myJPanel2(); add(p2,"Center"); myJPanel1 p1 = new myJPanel1(p2);//reverse the order of panel instantiation and pass p2 to panel1. add(p1,"North"); 
  3. Now you have reference to p2 in p1, if you if you could anything you like. 现在你可以参考p1中的p2,如果你有任何你喜欢的话。

  4. Use equals method to do comparison instead of == 使用equals方法进行比较而不是==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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