2012-12-29 38 views
2
String result = JOptionPane.showInputDialog(this, temp); 

result值將爲輸入值。使用內部對話框的JOptionPane問題

String result = JOptionPane.showInternalInputDialog(this, temp); 

result即使輸入字符串,值也將爲空。

temp是一個面板,將包含在JOptionPane中。這個JOptionPane將顯示在另一個定製的JOptioPane之上。

+0

我是指輸出打電話setBoundssetVisibleJPanel像我們那樣。爲什麼不同? –

+3

如果您很快就沒有得到體面的答案,請考慮創建併發布[sscce](http://sscce.org)。 –

回答

6

JOptionPane.showInternalInputDialog是根據僅JDesktopPane/JInternalFrame s,其中thisJDesktopPane/JInternalFrame小號實例中使用。

final JDesktopPane desk = new JDesktopPane(); 
... 
String s=JOptionPane.showInternalInputDialog(desk, "Enter Name"); 

如果不與任一它不會產生正確的輸出的2個上述部件的使用,實際上它會引發運行時異常:

了java.lang.RuntimeException:的JOptionPane:爲父級沒有 有效的父

UPDATE

根據你的意見,這裏是一個例子,你將如何添加JPanelJDesktopPane並致電JOptionPane#showInternalInputDialog。最重要的部分是我們需要的,如果它是JInternalFrame被添加到JDesktopPane,當然除了我們還添加了JPanel

JFrame frame = new JFrame("JInternalFrame Usage Demo"); 

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

// A specialized layered pane to be used with JInternalFrames 
jdpDesktop = new JDesktopPane() { 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(600, 600); 
    } 
}; 

frame.setContentPane(jdpDesktop); 

JPanel panel = new JPanel(); 
panel.setBounds(0, 0, 600, 600); 

jdpDesktop.add(panel); 

frame.pack(); 
frame.setVisible(true); 

panel.setVisible(true); 

String result = JOptionPane.showInternalInputDialog(jdpDesktop, "h"); 

System.out.println(result); 
+2

1+。你可能是對的,每當我看到'this'作爲這些方法之一的一個參數時,我就會不寒而慄,因爲它表明@Juan犯了一個錯誤,就是他的類不需要擴展JFrame或其他一些頂級GUI窗口。 –

+0

@HovercraftFullOfEels很好的調查技巧,很可能是正確的。 –

+0

它沒有顯示,我的代碼中的'this'是一個面板。 –