我看到这已被多次询问并提前道歉,如果我只是错过了一些简单的事情......
我创建了一个自定义JDialog,其中包含Java文档here中提供的示例,以及here提出的类似问题。
我的主应用程序是一个JFrame,它包含一个JPanel,其中包含一组显示各种员工姓名的JButtons。我已经为每个调用上述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 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的一个建议,对我来说没有意义)。再次感谢您的帮助。
答案 0 :(得分:0)
这就是继承的工作原理,你已经将statusDialog定义为JDialog引用,而JDialog没有getInputStatus方法。 要访问ChangeDialog的成员,您必须将statusDialog定义为ChangeDialog的变量。