String result = JOptionPane.showInputDialog(this, temp);
result
value 将是输入的值。
String result = JOptionPane.showInternalInputDialog(this, temp);
result
即使您输入了一个字符串,该值也将为 null。
temp
是将包含在 JOptionPane 中的面板。此 JOptionPane 将显示在另一个自定义的 JOptioPane 之上。
最佳答案
JOptionPane.showInternalInputDialog
仅与 JDesktopPane
/JInternalFrame
一起使用,其中 this
是 JDesktopPane
/JInternalFrame
的实例。
final JDesktopPane desk = new JDesktopPane();
...
String s=JOptionPane.showInternalInputDialog(desk, "Enter Name");
如果不与上述 2 个组件中的任何一个一起使用,它将不会产生正确的输出,实际上它会抛出一个运行时异常:
java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent
更新
根据您的评论,这里有一个示例,说明如何将 JPanel
添加到 JDesktopPane
并调用 JOptionPane#showInternalInputDialog
。重要的是我们需要在 JPanel
上调用 setBounds
和 setVisible
,就好像它是 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);
关于java - 使用内部对话框的 JOptionPane 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14083517/