Java JOptionPane:parentComponent没有有效的父级-正在尝试显示错误消息

Java JOptionPane:parentComponent没有有效的父级-正在尝试显示错误消息,java,Java,我正在尝试使用JOptionPane对话框获取用户输入。如果输入为空,我想显示错误消息,但无法使错误消息框正常工作 if (e.getSource() == atmDone) { String groupName = JOptionPane.showInputDialog("Enter the group name"); if (groupName.isEmpty()==false){ PrintStream output;

我正在尝试使用JOptionPane对话框获取用户输入。如果输入为空,我想显示错误消息,但无法使错误消息框正常工作

if (e.getSource() == atmDone) {
        String groupName = JOptionPane.showInputDialog("Enter the group name");

        if (groupName.isEmpty()==false){
            PrintStream output;
            try {
                output = new PrintStream(new File(groupName + ".txt"));
                output.println(textArea.getText());
                output.close();

                System.out.println("here");

            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
        } else if (groupName.isEmpty()){
            error = new JFrame();
            JOptionPane.showInternalMessageDialog(error, "Group couldn't be saved: name empty", "Error", JOptionPane.ERROR_MESSAGE);

        }
我收到的错误表明parentComponent没有有效的父级。 我应该使用什么作为父级以显示错误消息


谢谢。

您似乎误解了JOptionPane方法中父参数的用途。JOptionPane或与此相关的任何对话框都将与其父窗口相关联,因此,用户在任何时候聚焦父窗口时,都会显示该对话框。此外,提示对话框(如JOptionPane显示的对话框)通常是该父窗口的模态,这意味着它们阻止用户与父窗口交互,直到对话框关闭

出于这些原因,最好始终指定有效的父级。您的第一个JOptionPane语句应该是:

String groupName = JOptionPane.showInputDialog(atmDone.getTopLevelAncestor(), "Enter the group name");
您的第二个JOptionPane调用应该使用相同的父级。您看到的错误消息是由于您选择了错误的方法。所有名为
Internal
的JopOptions窗格方法都需要JDesktopPane作为提供的父级或父级,因为这些方法将其对话框显示为JInternalFrame

您可以使用名称不包含单词
Internal
的方法来解决此问题:

JOptionPane.showMessageDialog(atmDone.getTopLevelAncestor(),
    "Group couldn't be saved: name empty", "Error", JOptionPane.ERROR_MESSAGE);

您没有使用“内部框架”版本的
JOptionPane.showInputDialog
,那么为什么要使用错误消息执行此操作呢?String groupName=JOptionPane.showInputDialog(
null
,“输入组名”);