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

查看:80
本文介绍了在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代码相同。如果一个窗口打开另一个窗口(第二个窗口通常是对话框),则第一个窗口通常已经保存对第二个窗口的引用,并且可以调用其中的方法。关键通常是让第一个窗口调用第二个窗口来获取其状态时。如果第二个是模态对话框,那么时间很简单 - 在对话框返回后立即将您设置第二个对话框后立即显示在代码中。如果它不是模态对话框,那么你可能想要使用某种类型的监听器来知道何时提取信息。

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天全站免登陆