本文介绍了使用内部对话框的JOptionPane问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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一起使用,其中thisJDesktopPane/JInternalFrame s的实例

JOptionPane.showInternalInputDialog is to be used with JDesktopPane/JInternalFrames only, where this is the JDesktopPane/JInternalFrames 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上调用setBoundssetVisible,当然我们要添加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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 23:42