JOptionPane.showInternalInputDialog
是根據僅JDesktopPane
/JInternalFrame
s,其中this
是JDesktopPane
/JInternalFrame
小號實例中使用。
final JDesktopPane desk = new JDesktopPane();
...
String s=JOptionPane.showInternalInputDialog(desk, "Enter Name");
如果不與任一它不會產生正確的輸出的2個上述部件的使用,實際上它會引發運行時異常:
了java.lang.RuntimeException:的JOptionPane:爲父級沒有 有效的父
UPDATE
根據你的意見,這裏是一個例子,你將如何添加JPanel
到JDesktopPane
並致電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);
我是指輸出打電話
setBounds
和setVisible
上JPanel
像我們那樣。爲什麼不同? –如果您很快就沒有得到體面的答案,請考慮創建併發布[sscce](http://sscce.org)。 –