[英]Communicating between classes java
I have a class called myJPanel1
, in which I've created an instance of the class Student
. 我有一个名为
myJPanel1
的类,我在其中创建了Student
类的实例。 I've also created a button in myJPanel1
that displays info about this new student. 我还在
myJPanel1
中创建了一个按钮,显示有关这个新学生的信息。 When I click on that button, I want to change the text of a button in a separate JPanel
, written in class called myJPanel2
. 当我单击该按钮时,我想在单独的
JPanel
更改按钮的文本,该类在名为myJPanel2
类中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
识别的按钮myJPanel2
这样我就可以修改按钮的文本。
myJPanel1: 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: 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. 你需要的是建立某种形式的合同,允许
myJPanel1
改变的状态myJPanel2
不暴露任何部件的零件误操作。
For example, myJPanel1
shouldn't care what myJPanel2
wants to do with the informaiton, only that it can provide the information to it. 例如,
myJPanel1
不应该关心myJPanel2
想要对信息做什么,只是它可以向它提供信息。
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. 您可以建立一个模型,该模型可以更改各种属性并向感兴趣的各方提供适当的更改通知,或者您可以在
myJPanel2
提供一个方法,允许myJPanel1
提供您需要的信息。
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... 然后在你的
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());
}
}
}
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. 发生这种情况是因为你没有任何对按钮的引用,你创建它们并将它们分配给一个临时引用,当构造函数完成时它的作业
j1
将没有任何生存变量来保存它们。
You should move the buttons into your JPanel
class. 您应该将按钮移动到
JPanel
类中。
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. 评论:如果您想在全班使用它的按钮,
myJPanel1
如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.