在两个JPanel对象之间发送消息

编程入门 行业动态 更新时间:2024-10-23 08:26:25
本文介绍了在两个JPanel对象之间发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-11-25 22:41:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送消息   对象   两个   JPanel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!