簡體   English   中英

在 JFrame 之間傳遞值

[英]Passing values between JFrames

我有兩個 Jframe,其中 frame1 有一些文本字段,當單擊 frame1 上的按鈕時,我打開另一個 JFrame,其中包含一個搜索框和一個包含搜索結果的 JTable。

當我單擊 JTable 上的結果行時,我希望該特定值反映在 frame1 文本字段中。

我嘗試將 JFrame1 的 object 作為參數傳遞,但我不清楚如何實現這一點。 任何幫助將不勝感激。 謝謝

首先,您的程序設計似乎有點偏離,就好像您正在為您的 windows 之一使用 JFrame ,實際上您應該使用 JDialog 因為它聽起來好像一個 Z05B8C74CBD96FBF2DE44C1A352702 應該依賴於其他。

但無論如何,您傳遞 GUI 對象的引用與傳遞標准非 GUI Java 代碼相同。 如果一個 window 打開另一個(第二個通常是對話框),那么第一個 window 通常已經擁有對第二個 window 的引用,並且可以調用它的方法。 關鍵通常是什么時候讓第一個 window 調用第二個方法來獲取它的 state。 如果第二個是模態對話框,那么什么時候很容易 - 對話框返回后立即出現,在您將第二個對話框設置為可見后立即出現在代碼中。 如果它不是模態對話框,那么您可能希望使用某種偵聽器來了解何時提取信息。

話雖如此,細節都將取決於您的程序結構,如果您需要更具體的幫助,您需要告訴我們更多相關信息。

舉個簡單的例子,有一個 window 打開另一個,允許用戶在對話框 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM