Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在两个JPanel对象之间发送消息_Java_Oop_Swing_User Interface_Jpanel - Fatal编程技术网

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

Java 在两个JPanel对象之间发送消息,java,oop,swing,user-interface,jpanel,Java,Oop,Swing,User Interface,Jpanel,我有一个包含JPanel的JavaJFrame。在这个JPanel中,有两个独立的JPanel。当用户单击第一个JPanel中的按钮时,需要向另一个JPanel发送一条消息,通知它单击了哪个按钮。在这样的对象之间发送消息的最简单方法是什么?您可以创建一个自定义事件,并将一个或多个侦听器附加到它 正确的实现方法是让按钮ActionListener触发事件,然后让您的两个面板成为该事件的侦听器。位于类的顶部 public MyClass implements ActionListener { ..

我有一个包含JPanel的JavaJFrame。在这个JPanel中,有两个独立的JPanel。当用户单击第一个JPanel中的按钮时,需要向另一个JPanel发送一条消息,通知它单击了哪个按钮。在这样的对象之间发送消息的最简单方法是什么?

您可以创建一个自定义事件,并将一个或多个侦听器附加到它


正确的实现方法是让按钮ActionListener触发事件,然后让您的两个面板成为该事件的侦听器。

位于类的顶部

public MyClass implements ActionListener {

...
myButton.addActionListener(this);
...

public void actionPerformed(ActionEvent e) {
      //for example if you have more than one events that you need to handle
      if(e.getSource().equals(myButton) {
           //update your do some work on you jpanels
      }
}

但实际上,我认为是时候开始考虑设计模式了。您所描述的是模式的完美候选者,也可能是模式的完美候选者,例如使用类之间的构造函数,或者(对于调试问题)使用
getParent()从所需的
JComponent
(s)提取值

+

+

+

import java.awt.*;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
公共类CopyTextSouthPanel扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私有JTextField firstText;
私有JButton复制按钮;
私有JTextField sourceTextField;
私有字符串lds=“”;
公共CopyTextSouthPanel(){
firstText=新的JTextField(“所需的TextField”);
firstText.setMinimumSize(新尺寸(300,25));
setPreferredSize(新维度(300,25));
setMaximumSize(新尺寸(300,25));
copyButton=新的JButton(“从JTextFields复制文本”);
copyButton.setMinimumSize(新尺寸(200,25));
copyButton.setPreferredSize(新尺寸(200,25));
copyButton.setMaximumSize(新尺寸(200,25));
copyButton.addActionListener(新java.awt.event.ActionListener(){
@凌驾
public void actionPerformed(java.awt.event.ActionEvent evt){
copyButtonActionPerformed(evt);
}
私有无效复制按钮执行(ActionEvent evt){
系统输出打印(“按下按钮”+“\n”);
Component[]comp=CopyTextSouthPanel.this.getParent().getComponents();
int nO=组件长度;
对于(int i=0;i”+str+“\n”);
打破
}
}
}
}
打破
}
}
}
lds=sourceTextField.getText();
if(lds!=null | |(!(lds.isEmpty())){
setText(“Msg->”+lds);
}
}
});
添加(第一个文本,BorderLayout.EAST);
添加(复制按钮,BorderLayout.WEST);
}
public void setSourceTextField(JTextField源){
this.sourceTextField=source;
}
}
对于mKorbel(以及原始海报):
我建议的是更松散的耦合,一个JPanel不知道另一个JPanel,所有连接都是通过某种控制来完成的。例如,要借用一些代码:

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();
   }
}
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);
   }
}
CopyTextSouthPanel2.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();
   }
}
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

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

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彼此不知道,并根据其他答案使用侦听器。@满是Eels的气垫船取决于您的评论很难说出明智的话,构造函数和最终的Getter/Setter也都是硬编码的,我认为这两个都仍然被视为JComponents的基本内容,对我来说也是一样的,如果在每个连接上都发生了错误,那么创建对象类的方法请查看我的答案以理解我的意思。另请参见。链接()似乎已经过时了,不包括电池,但我仍然可以看到,在我的时区中,相同的时间是午夜后超过2小时,睡眠时间+1感谢课程,同意如果存在多个连接,则最好…+1建议松耦合-0.5建议不够远:-)@HovercraftFullOfEels已测试并运行!这种技术有名字吗?此外,这与观察者模式不同还是相同?
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;
   }

}
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();
         }
      });
   }
}