我有一个包含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对象之间发送消息
发布评论