问题描述
String result = JOptionPane.showInputDialog(this, temp);
result
值为输入值.
String result = JOptionPane.showInternalInputDialog(this, temp);
即使输入了字符串,
result
的值也将为空.
result
value will be null even you inputted a string.
temp
是将包含在JOptionPane中的面板.此JOptionPane将显示在另一个自定义的JOptioPane顶部.
temp
is a panel that will contain in the JOptionPane. This JOptionPane will show on top of another customized JOptioPane.
推荐答案
JOptionPane.showInternalInputDialog
仅与JDesktopPane
/JInternalFrame
s一起使用,其中this
是JDesktopPane
/JInternalFrame
s的实例
JOptionPane.showInternalInputDialog
is to be used with JDesktopPane
/JInternalFrame
s only, where this
is the JDesktopPane
/JInternalFrame
s instance.
final JDesktopPane desk = new JDesktopPane();
...
String s=JOptionPane.showInternalInputDialog(desk, "Enter Name");
如果不与上述两个组件中的任何一个一起使用,它将不会产生正确的输出,实际上,它将引发运行时异常:
If not used with either of the 2 above mentioned components it will not produce the correct output, in fact it will throw a Runtime Exception:
更新
根据您的评论,这里有一个示例,说明如何将JPanel
添加到JDesktopPane
并调用JOptionPane#showInternalInputDialog
.重要的是,我们需要像在JInternalFrame
中添加JInternalFrame
一样在JPanel
上调用setBounds
和setVisible
,当然我们要添加JPanel
As per your comments here is an example of how you would add JPanel
to JDesktopPane
and call JOptionPane#showInternalInputDialog
. The important part is we need to call setBounds
and setVisible
on JPanel
like we would as if it was JInternalFrame
being added to the JDesktopPane
, except of course we are adding a 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);
这篇关于使用内部对话框的JOptionPane问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!