在 JFrame 之间传递值 [英] Passing values between JFrames

查看:54
本文介绍了在 JFrame 之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two Jframes where frame1 has some text fields and when a button on frame1 is clicked, I open another JFrame which contains a search box and a JTable containing search results.

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

When I click on a result row on JTable, I want that particular values to be reflected in the frame1 text fields.

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

I tried passing the JFrame1's object as a parameter but I have no clear idea on how to achieve this. Any help would be highly appreciated. Thanks

推荐答案

首先,你的程序设计似乎有点偏离,好像你在你的一个窗口中使用了 JFrame,而实际上你应该使用 JDialog因为听起来好像一个窗口应该依赖于另一个.

First of all, your program design seems a bit off, as if you are using a JFrame for one of your windows where you should in fact be using a JDialog since it sounds as if one window should be dependent upon the other.

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

But regardless, you pass references of GUI objects the same as you would standard non-GUI Java code. If one window opens the other (the second often being the dialog), then the first window usually already holds a reference to the second window and can call methods off of it. The key often is when to have the first window call the second's methods to get its state. If the second is a modal dialog, then the when is easy -- immediately after the dialog returns which will be in the code immediately after you set the second dialog visible. If it is not a modal dialog, then you probably want to use a listener of some sort to know when to extract the information.

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

Having said this, the details will all depend on your program structure, and you'll need to tell us more about this if you want more specific help.

一个简单的例子,一个窗口打开另一个窗口,允许用户在对话窗口 JTextField 中输入文本,然后将文本放置在第一个窗口的 JTextField 中,请看这个:

For a simple example that has one window open another, allows the user to enter text into the dialog windows JTextField, and then places the text in the first window's JTextField, please have a look at this:

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 中获取信息.

You'd do a very similar technique to get information out of a JTable.

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

And again, if this information doesn't help you, then please tell us more about your program including showing us some of your code. The best code to show is a small compilable example, an SSCCE similar to what I've posted above.

这篇关于在 JFrame 之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆