java - 在 JFrame 之间传递值

标签 java swing jframe

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

当我单击 JTable 上的结果行时,我希望该特定值反射(reflect)在 frame1 文本字段中。

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

最佳答案

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

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

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

一个简单的例子,一个窗口打开另一个,允许用户在对话框窗口的 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类似于我上面发布的内容。

关于java - 在 JFrame 之间传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7017063/

相关文章:

java - JTabbedPane 无法正常工作

java - addNotify() 和 requestFocus() 在 Java 中如何与 JPanel 一起工作?

java - 为什么我的 JFrame 图标没有从默认的 java 图标更改?

java - 无法从浏览器访问嵌入式 Glassfish 的运行实例

java - 如何获取通用树中节点的级别?

java - 如何找出 JFrame 的实际大小?

Java swing jTable 未更新

java - 重新验证 jframe 的问题

java - 在 JFrame 中实现 CardLayout 并根据特定按钮按下切换卡片

java - JavaSpring 中的 SQL 查询返回 Null