【问题标题】:Passing values between JFrames在 JFrame 之间传递值
【发布时间】:2011-10-24 10:17:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: java swing jframe


    【解决方案1】:

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

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

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

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

    【讨论】:

    • +1 表示耐心,当之无愧的Java tag badge
    • 有关在对话框中使用组件的另一个更通用的示例,请参阅ConfirmDialog.java
    • 非常感谢您的详细解释。我在第二个窗口中使用了 JDialog,它工作正常。你真的很有帮助。
    • 天啊,我可能应该回到 C#。这么多噪音只是为了从对话框窗口中获得结果
    • @electricalbah:它还提出了 JavaFx 如何处理这个问题,因为这个库现在应该取代 Swing。对于它的价值,我不知道答案。
    猜你喜欢
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2012-04-14
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多