在jframes之间传递值

k2arahey  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(230)

我有两个jframe,其中frame1有一些文本字段,当单击frame1上的按钮时,我打开另一个jframe,其中包含一个搜索框和一个包含搜索结果的jtable。
当我单击jtable上的结果行时,我希望特定的值反映在frame1文本字段中。
我尝试将jframe1的对象作为参数传递,但我不清楚如何实现这一点。任何帮助都将不胜感激。谢谢

xa9qqrwz

xa9qqrwz1#

首先,您的程序设计似乎有点不对劲,好像您正在为一个窗口使用jframe,而实际上您应该使用jdialog,因为听起来好像一个窗口应该依赖于另一个窗口。
但不管怎样,传递gui对象的引用与传递标准非gui java代码相同。如果一个窗口打开另一个窗口(第二个窗口通常是对话框),那么第一个窗口通常已经保存了对第二个窗口的引用,并且可以从中调用方法。关键通常是何时让第一个窗口调用第二个窗口的方法来获取其状态。如果第二个是模态对话框,那么when很容易——在对话框返回之后立即返回,在您将第二个对话框设置为可见之后,它将立即出现在代码中。如果它不是模态对话框,那么您可能需要使用某种侦听器来知道何时提取信息。
话虽如此,细节将取决于您的程序结构,如果您需要更具体的帮助,您需要告诉我们更多关于这一点。
对于一个简单的示例,一个窗口打开另一个窗口,允许用户在对话框windows 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中获取信息。
再说一次,如果这些信息对你没有帮助,那么请告诉我们更多关于你的程序的信息,包括给我们看一些你的代码。最好的代码是一个小的可编译示例,一个类似于我上面发布的sscce。

相关问题