将值或访问从 jDialog 传递到 jFrame,反之亦然 [英] pass value or access from jDialog to jFrame and vice-versa

查看:45
本文介绍了将值或访问从 jDialog 传递到 jFrame,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我确实阅读了您发布的链接,但出现以下错误:

OK I did read the link you posted but I get these errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at client.medical.main.Main.jMenuItem6ActionPerformed(Main.java:348)
at client.medical.main.Main.access$400(Main.java:21)
at client.medical.main.Main$5.actionPerformed(Main.java:249)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

我编辑了我的代码,所以它看起来像你给我的链接中的那个.

I edited my code so it would look like the one in the link you given me.

JDialog:我刚刚添加了一个新的 JButton,就像这样:

JDialog : I just added one new JButton just like this:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

    Window win = SwingUtilities.getWindowAncestor(this);
    if (win != null) {
        win.dispose();
     }
   }
  public String getFieldText() {
     return jTextField1.getText();
  }

JFrame:这里我创建了 2 个变量 dialogPanel 和 dialog 然后编辑了打开 jdialog 的按钮,代码如下:

JFrame: here I made 2 variables dialogPanel and dialog then edited the button that open jdialog here is the code:

 private Recherche dialogPanel = new Recherche();
 private JDialog dialog;    
 private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  if (dialog == null) {
     Window win = SwingUtilities.getWindowAncestor(this);
      if (win != null) {
        dialog = new JDialog(win, "My Dialog",
                 Dialog.ModalityType.APPLICATION_MODAL);
        dialog.getContentPane().add(dialogPanel);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
     }
  }
  dialog.setVisible(true); // here the modal dialog takes over
  System.out.print(dialogPanel.getFieldText());

}

我知道我在这里弄乱了一些东西但我看不到它,你能帮我解决这个问题吗?

I know I am messing something here but I can't see it, could you please help me solve this problem?

推荐答案

对于初学者,不要使用 JDialog#show() 因为该方法已被弃用(请检查 JDialog API 以了解更多关于这个).而是使用 JDialog#setVisible(true).

For starters, don't use JDialog#show() since that method has been deprecated (please check the JDialog API to see more on this). Instead use JDialog#setVisible(true).

这段代码是从主框架"中调用的吗?

Is this code being called from within the "main frame"?

如果是这样,由于您的对话框是模态的,您可以在将其设置为可见后立即在代码中查询对话框实例以了解其变量的状态.您可以使用 getter(也称为访问器)方法来提取此信息.否则,您需要拥有对主框架"实例的有效引用才能将信息传入.

If so, since your dialog is modal, you can simply query the dialog instance for the state of its variables in the code immediately after you've set it visible. You could use getter (also known as accessor) methods to extract this information. Otherwise you'll need to have a valid reference to the "main frame" instance to pass the information in.

如需更多帮助,请告诉我们更多有关您的问题的信息.另外,请注意,我很少有扩展 JFrame 或 JDialog 的类,而是在需要时创建我的 JFrame 或 JDialog 来保存我的 GUI,这些 GUI 旨在创建 JPanel 以获得更大的灵活性.

For more help, please tell us more about your problem. Also, please note that I rarely have classes that extend JFrame or JDialog but instead create my JFrame or JDialog when needed to hold my GUI's which are geared towards creating JPanels for more flexibility.

编辑
关于您最近的编辑,我仍然无法确切地看到您卡在哪里,但让我们简化问题,让您尝试从 JDialog 中的 JTextField 获取信息,并使用该信息填充 JFrame 中的 JTextField.我会给对话框类一个 getFieldText() 方法,该方法返回其字段中保存的文本,然后我让 JFrame 在 dialog 实例上调用这个方法, 在对话框返回后.

Edit
Regarding your recent edit, I still can't see exactly where you're stuck, but let's simplify the problem and just have you try to get the information from a JTextField in the JDialog and use that information to populate a JTextField in the JFrame. I'd give the dialog class a getFieldText() method that returns the text held in its field, and then I'd have the JFrame call this method on the dialog instance, after the dialog returns.

有关具体示例,请参阅我在此答案中发布的代码此处.

For a specific example of this, please see the code I posted in this answer here.

编辑 2
例如,使用您的代码,一切正常.因此,如果您仍然遇到问题,则必须向我们展示更多信息:

Edit 2
For example, using your code, everything works. So if you're still having trouble, you have to show us more:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class DialogEg {
   private static void createAndShowGUI() {
      MainPanelGen mainPanelGen = new MainPanelGen();

      JFrame frame = new JFrame("DialogEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanelGen.getMainPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }

}

class MainPanelGen {
   private JPanel mainPanel = new JPanel();
   private JTextField field = new JTextField(10);
   private JButton btn = new JButton(new BtnActn());
   private JDialog dialog;
   private DialogPanel dialogPanel = new DialogPanel();

   public MainPanelGen() {
      mainPanel.add(field);
      mainPanel.add(btn);

      field.setEditable(false);
      field.setFocusable(false);
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   private class BtnActn extends AbstractAction {
      BtnActn() {
         super("Button");
      }

      @Override
      public void actionPerformed(ActionEvent arg0) {
         if (dialog == null) {
            Window win = SwingUtilities.getWindowAncestor(mainPanel);
            if (win != null) {
               dialog = new JDialog(win, "My Dialog",
                     Dialog.ModalityType.APPLICATION_MODAL);
               dialog.getContentPane().add(dialogPanel);
               dialog.pack();
               dialog.setLocationRelativeTo(null);
            }
         }
         dialog.setVisible(true); // here the modal dialog takes over
         System.out.println   (dialogPanel.getFieldText());
         field.setText(dialogPanel.getFieldText());
      }
   }
}

class DialogPanel extends JPanel {
   private JTextField field = new JTextField(10);
   private JButton exitBtn = new JButton(new ExitBtnAxn("Exit"));

   public DialogPanel() {
      add(field);
      add(exitBtn);
   }

   public String getFieldText() {
      return field.getText();
   }

   private class ExitBtnAxn extends AbstractAction {

      public ExitBtnAxn(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent arg0) {
         Window win = SwingUtilities.getWindowAncestor(DialogPanel.this);
         if (win != null) {
            win.dispose();
         }

      }

   }

}

您告诉我们的还不够多,无法让我们为您提供帮助.什么线路导致NPE?您是否测试了该行上的变量以查看哪些变量为空?您是否回顾过您的代码以了解为什么变量为空?

You have not told us enough to let us help you. What line causes the NPE? Have you tested the variables on that line to see which one(s) are null? Have you looked back into your code to see why the variable(s) are null?

这篇关于将值或访问从 jDialog 传递到 jFrame,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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