2012-09-11 81 views
-1

OK我沒有看到你發佈的鏈接,但我得到這些錯誤:通值或從的JDialog到JFrame的訪問,反之亦然

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) 

我修改了代碼,所以它看起來像一個的鏈接你給了我。

的JDialog:我只是增加了一個新的JButton就像這樣:

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,然後對話框中進行編輯,在這裏打開的JDialog按鈕是代碼:

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()); 

} 

我知道我在這裏搞點東西,但我看不到它,請問您能幫我解決這個問題嗎?

+0

你還沒有告訴我們足夠讓我們來幫助你。什麼線路導致NPE?你是否測試了該行的變量以查看哪一個是空的?你回頭看看你的代碼,看看爲什麼變量是空的? –

+0

'client.medical.main.Main.jMenuItem6ActionPerformed(Main.java:348)'表示NullPointerException(NPE)發生在文件Main.java的第348行。您發佈的代碼中的第348行在哪裏? –

回答

1

對於初學者,請勿使用JDialog#show(),因爲該方法已被棄用(請檢查JDialog API以瞭解更多信息)。請使用JDialog#setVisible(true)

此代碼正在從「主框架」內叫什麼名字?

如果是這樣,因爲你的對話框是模式的,你可以簡單地查詢對話框中的實例作爲代碼變量的狀態,你將它設置可見之後。您可以使用getter(也稱爲訪問器)方法來提取此信息。否則,您需要有一個有效的「主框架」實例來傳遞信息。

如需更多幫助,請告訴我們更多關於您的問題。另外,請注意,我很少有類可以擴展JFrame或JDialog,而是在需要時創建我的JFrame或JDialog來保存我的GUI,以便創建更加靈活的JPanel。

編輯
關於你最近的編輯,我現在還不能清楚地看到你就完蛋了,但讓我們簡化問題,只是有你嘗試從一個JTextField獲取信息的的JDialog和使用該信息來填充JFrame中的JTextField。我會給對話類一個getFieldText()方法,它返回其字段中保存的文本,然後我會讓對話框返回後,JFrame在對話框實例上調用此方法。

有關具體示例,請參閱我在此答案中發佈的代碼here

編輯2
例如,使用你的代碼,一切正常。所以,如果你仍然有問題,你必須向我們展示更多:

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?你是否測試了該行的變量以查看哪一個是空的?你回頭看看你的代碼,看看爲什麼變量是空的?

+0

我剛更新了我的帖子,更多信息 – user1559104

+0

@ user1559104:請參閱編輯以回答以及鏈接,以獲取我描述的具體示例。 –

+0

氣墊船充滿了鰻魚:我的主要問題是,我有jpaialog中的jpanel,所以我真的不能打電話給方法 – user1559104