Return 来自自定义 JDialog 的值

Return value from custom JDialog

我看到这个问题已经被问过很多次了,如果我只是遗漏了一些简单的东西,请提前道歉...

我使用 Java 文档 here and from a similar question asked here.

中提供的示例创建了自定义 JDialog

我的主要应用程序是一个 JFrame,它包含一个 JPanel 和一个显示各种员工姓名的 JButton 数组。我已经为每个调用提到的 JDialog:

的 JButton 添加了自定义 ActionListener
//inner class for handling user button pushes
private class UserButtonHandler implements ActionListener
{
    //handle button event
    @Override
    public void actionPerformed(ActionEvent event)
    {
        statusDialog = new ChangeDialog(statusWindow);
        statusDialog.setVisible(true);
        statusDialog.setLocationRelativeTo(null);

        //set title for dialog box
        String dialogTitle = "Status change for " + event.getActionCommand();
        statusDialog.setTitle(dialogTitle);

        statNum = ((ChangeDialog) statusDialog).getInputStatus();
        System.out.println("Current num is: " + statNum);

        //statNum = statusDialog.getInputStatus();

    }
}

这是自定义 JDialog (ChangeDialog) 的 class:

class ChangeDialog extends JDialog implements ActionListener, PropertyChangeListener
{
    //create panel where users can modify their status
    private final ChangePanel empStatusChangePanel;

    //text of buttons in dialog
    private String btnString1 = "OK";
    private String btnString2 = "Cancel";
    private String btnString3 = "Clear Time-Off";

    private JOptionPane statusPane;

    //determines message to return for user input
    private int inputStatus; 

    public ChangeDialog(JFrame statusFrame)
    {
        empStatusChangePanel = new ChangePanel();

        //create an array specifying the number
        //of dialog buttons and their text
        Object[] options = {btnString1, btnString2, btnString3};

        //create the JOptionPane
        statusPane = new JOptionPane(empStatusChangePanel,
                JOptionPane.PLAIN_MESSAGE,
                JOptionPane.YES_NO_CANCEL_OPTION,
                null,
                options,
                options[0]);

        //set contents of dialog
        setContentPane(statusPane);

        //handle window closing
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //register event handler for changes in status pane state
        statusPane.addPropertyChangeListener(this);
        pack();

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        statusPane.setValue(btnString1);
    }

    @Override
    public void propertyChange(PropertyChangeEvent e)
    {
        String prop = e.getPropertyName();

        if (isVisible()
                && (e.getSource() == statusPane)
                && (JOptionPane.VALUE_PROPERTY.equals(prop)))
        {
            Object value = statusPane.getValue();

            if (value == JOptionPane.UNINITIALIZED_VALUE)
            {
                //ignore reset
                return;
            }

            //Reset the JOptionPane's value. If this is not done,
            //then if the user presses the same button next time,
            //no property change event will be fired
            statusPane.setValue(JOptionPane.UNINITIALIZED_VALUE);

            if(value.equals(btnString1)) //user clicked "OK"
            {
                //validation of user input
                inputStatus = empStatusChangePanel.validateUserInput();

                //handle validation results
                switch (inputStatus)
                {
                case 0: //user input is good
                    JOptionPane.showMessageDialog(this, "Good input given");
                    dispose();
                    break;

                case 1: //one (or both) of the date pickers are empty
                    JOptionPane.showMessageDialog(this, "PTO pickers can't be empty.",
                            "ERROR", JOptionPane.ERROR_MESSAGE);
                    break;

                case 2:
                case 3: //bad date range (start before end or visa-versa)
                    JOptionPane.showMessageDialog(this, "Bad date range.",
                            "ERROR", JOptionPane.ERROR_MESSAGE);
                    break;

                case 99: //dates are equal
                    JOptionPane.showMessageDialog(this, "Single-day PTO");
                    dispose();
                    break;
                }
            }
            else if(value.equals(btnString3)) //user clicked "Clear Input"
            {
                JOptionPane.showMessageDialog(this, "User clicked 'clear input");
                //more processing should be done here
                empStatusChangePanel.recycle();

                //dispose();
            }
            else //user clicked "Cancel" or closed dialog
            {
                JOptionPane.showMessageDialog(this,  "User closed status window");
                dispose();
            }
        }
    }

    //returns value from user validation
    public int getInputStatus()
    {
        return inputStatus;
    }

}

我需要从自定义对话框访问方法 getInputStatus,但我尝试过的每次尝试都会返回说明:

getInputStatus is undefined for the type JDialog

我看过其他几篇类似的帖子,但觉得我在尝试解决这个问题时遗漏了一些基本的东西(或者我看代码的时间太长了)。

另一件让我难过的事情(以及为什么我把它留在第一个片段中)是,如果我将方法转换为 ChangeDialog

类型
statNum = ((ChangeDialog) statusDialog).getInputStatus();

它突然可以访问(这是 Eclipse 的建议,对我来说没有意义)。再次感谢所有帮助。

这就是继承的工作原理,您已将 statusDialog 定义为 JDialog 引用,而 JDialog 没有 getInputStatus 方法。 要访问 ChangeDialog 的成员,您必须将 statusDialog 定义为 ChangeDialog 的变量。