Java 在JFrames之间传递值

Java 在JFrames之间传递值,java,swing,jframe,Java,Swing,Jframe,我有两个JFrame,其中frame1有一些文本字段,当单击frame1上的按钮时,我打开另一个JFrame,其中包含一个搜索框和一个包含搜索结果的JTable 当我单击JTable上的结果行时,我希望特定值反映在frame1文本字段中 我尝试将JFrame1的对象作为参数传递,但我不清楚如何实现这一点。 任何帮助都将不胜感激。 谢谢首先,您的程序设计似乎有点不对劲,好像您正在为其中一个窗口使用JFrame,实际上您应该使用JDialog,因为听起来好像一个窗口应该依赖于另一个窗口 但无论如何,

我有两个JFrame,其中frame1有一些文本字段,当单击frame1上的按钮时,我打开另一个JFrame,其中包含一个搜索框和一个包含搜索结果的JTable

当我单击JTable上的结果行时,我希望特定值反映在frame1文本字段中

我尝试将JFrame1的对象作为参数传递,但我不清楚如何实现这一点。 任何帮助都将不胜感激。
谢谢

首先,您的程序设计似乎有点不对劲,好像您正在为其中一个窗口使用JFrame,实际上您应该使用JDialog,因为听起来好像一个窗口应该依赖于另一个窗口

但无论如何,传递GUI对象的引用与传递标准非GUI Java代码的方式相同。如果一个窗口打开另一个窗口(第二个窗口通常是对话框),那么第一个窗口通常已经包含对第二个窗口的引用,并且可以从中调用方法。关键通常是何时让第一个窗口调用第二个窗口的方法来获取其状态。如果第二个对话框是模态对话框,那么when很容易——在对话框返回后立即返回,这将在您设置第二个对话框可见后立即出现在代码中。如果它不是模态对话框,那么您可能希望使用某种侦听器来了解何时提取信息

话虽如此,细节将取决于您的程序结构,如果您需要更具体的帮助,您需要告诉我们更多有关这方面的信息

对于一个简单示例,一个窗口打开另一个窗口,允许用户在对话框窗口JTextField中输入文本,然后将文本放置在第一个窗口的JTextField中,请查看以下内容:

import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class WindowCommunication {

   private static void createAndShowUI() {
      JFrame frame = new JFrame("WindowCommunication");
      frame.getContentPane().add(new MyFramePanel());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   // let's be sure to start Swing on the Swing event thread
   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

class MyFramePanel extends JPanel {
   private JTextField field = new JTextField(10);
   private JButton openDialogeBtn = new JButton("Open Dialog");

   // here my main gui has a reference to the JDialog and to the
   // MyDialogPanel which is displayed in the JDialog
   private MyDialogPanel dialogPanel = new MyDialogPanel();
   private JDialog dialog;

   public MyFramePanel() {
      openDialogeBtn.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            openTableAction();
         }
      });
      field.setEditable(false);
      field.setFocusable(false);

      add(field);
      add(openDialogeBtn);
   }

   private void openTableAction() {
      // lazy creation of the JDialog
      if (dialog == null) {
         Window win = SwingUtilities.getWindowAncestor(this);
         if (win != null) {
            dialog = new JDialog(win, "My Dialog",
                     ModalityType.APPLICATION_MODAL);
            dialog.getContentPane().add(dialogPanel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
         }
      }
      dialog.setVisible(true); // here the modal dialog takes over

      // this line starts *after* the modal dialog has been disposed
      // **** here's the key where I get the String from JTextField in the GUI held
      // by the JDialog and put it into this GUI's JTextField.
      field.setText(dialogPanel.getFieldText());
   }
}

class MyDialogPanel extends JPanel {
   private JTextField field = new JTextField(10);
   private JButton okButton = new JButton("OK");

   public MyDialogPanel() {
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            okButtonAction();
         }
      });
      add(field);
      add(okButton);
   }

   // to allow outside classes to get the text held by the JTextField
   public String getFieldText() {
      return field.getText();
   }

   // This button's action is simply to dispose of the JDialog.
   private void okButtonAction() {
      // win is here the JDialog that holds this JPanel, but it could be a JFrame or 
      // any other top-level container that is holding this JPanel
      Window win = SwingUtilities.getWindowAncestor(this);
      if (win != null) {
         win.dispose();
      }
   }
}
您可以使用非常类似的技术从JTable中获取信息


同样,如果这些信息对您没有帮助,请告诉我们更多关于您的程序的信息,包括向我们展示您的一些代码。最好的代码是一个小的可编译示例,类似于我上面发布的内容。

+1耐心等待,这是一个当之无愧的示例。有关在对话框中使用组件的另一个更通用的示例,请参阅。非常感谢您的详细解释。我在第二个窗口中使用了JDialog,它工作得很好。你真的帮了大忙。天哪,我可能应该回到C。这么多噪音只是为了从对话中得到结果window@electricalbah:它也提出了JavaFx如何处理这个问题,因为这个库现在应该取代Swing。无论如何,我不知道这个问题的答案。