在两个 JPanel 对象之间发送消息 [英] Sending messages between two JPanel objects
问题描述
我有一个包含 JPanel 的 Java JFrame.在该 JPanel 中,有两个独立的 JPanel.当用户单击第一个 JPanel 中的按钮时,需要向另一个 JPanel 发送一条消息,通知它单击了哪个按钮.在这样的对象之间发送消息的最简单方法是什么?
I have a Java JFrame containing a JPanel. Within that JPanel, there are two separate JPanels. When the user clicks a button in the first JPanel, a message needs to be sent to the other JPanel notifying it which button was clicked. What is the easiest way to send messages between objects like this?
推荐答案
对于 mKorbel(和原始海报):
我推荐的是松散耦合,即一个 JPanel 不知道另一个 JPanel,并且所有连接都是通过某种控制完成的.例如,借用你的一些代码:
For mKorbel (and the original poster):
What I'm recommending is looser coupling, that the one JPanel has no knowledge of the other JPanel and that all connections are done through a control of some sort. For instance, to borrow some of your code:
CopyTextNorthPanel2.java
CopyTextNorthPanel2.java
import java.awt.*;
import javax.swing.*;
public class CopyTextNorthPanel2 extends JPanel {
private static final long serialVersionUID = 1L;
public JTextField northField;
public CopyTextNorthPanel2() {
northField = new JTextField("Welcome World");
northField.setFont(new Font("Serif", Font.BOLD, 20));
northField.setPreferredSize(new Dimension(300, 25));
add(northField);
}
public String getNorthFieldText() {
return northField.getText();
}
}
CopyTextSouthPanel2.java
CopyTextSouthPanel2.java
import java.awt.event.*;
import javax.swing.*;
public class CopyTextSouthPanel2 extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField firstText = new JTextField("Desired TextField");
private JButton copyButton = new JButton("Copy text from JTextFields");
private CopyTextControl2 control;
public CopyTextSouthPanel2() {
copyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (control != null) {
control.copyAction();
}
}
});
add(firstText);
add(copyButton);
}
public void setControl(CopyTextControl2 control) {
this.control = control;
}
public void setFirstText(String text) {
firstText.setText(text);
}
}
CopyTextControl2.java
CopyTextControl2.java
public class CopyTextControl2 {
private CopyTextNorthPanel2 northPanel;
private CopyTextSouthPanel2 southPanel;
public void copyAction() {
if (northPanel != null && southPanel != null) {
southPanel.setFirstText(northPanel.getNorthFieldText());
}
}
public void setNorthPanel(CopyTextNorthPanel2 northPanel) {
this.northPanel = northPanel;
}
public void setSouthPanel(CopyTextSouthPanel2 southPanel) {
this.southPanel = southPanel;
}
}
CopyText2.java
CopyText2.java
import java.awt.*;
import javax.swing.*;
public class CopyText2 {
private static void createAndShowUI() {
CopyTextNorthPanel2 northPanel = new CopyTextNorthPanel2();
CopyTextSouthPanel2 southPanel = new CopyTextSouthPanel2();
CopyTextControl2 control = new CopyTextControl2();
southPanel.setControl(control);
control.setNorthPanel(northPanel);
control.setSouthPanel(southPanel);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(northPanel, BorderLayout.NORTH);
mainPanel.add(Box.createRigidArea(new Dimension(100, 100)), BorderLayout.CENTER);
mainPanel.add(southPanel, BorderLayout.SOUTH);
JFrame frame = new JFrame("Copy Text");
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
这篇关于在两个 JPanel 对象之间发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!