当我在两个 windows 之间切换时 JDialog 消失了
JDialog dissappears when I switch between two windows
我正在尝试制作一个简单的 JDialog,它要求用户以 3 个文本字段的形式输入,并且它显示正确并且它的 PropertyListener 工作得很好,我还没有为 JDialog 分配父级在它的构造函数中,所以我猜测默认情况下父级设置为我的小程序中所有组件的祖先。但是,当我从 applet 更改为 firefox window 并且当我返回我的 applet 时,JDialog 消失了。我是否需要为 JDialog 设置某个 属性 以确保即使在我切换 windows 时它仍然存在。令人震惊的是,我认为该对话框仍在运行,但不可见,因为当第一个对话框消失后另一个对话框出现时,两个对话框同时出现(第一个对话框重新出现)。我的 JDialog 代码如下:
private void addQuestion() {
questionTextField = new TextField(50);
Object[] componentsArray = {"Question:", questionTextField, "MQLYes:", mqlYesTextField, "MQLNo:", mqlNoTextField};
Object[] options = {"Enter", "Cancel"};
addQuestionDialog = new JDialog(new JFrame(),"Add question");
addQuestionPane = new JOptionPane(componentsArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[0]);
int x = getX() + getWidth()/2, y = getY() + getHeight()/2;
addQuestionDialog.setContentPane(addQuestionPane);
addQuestionDialog.setResizable(false);
addQuestionDialog.setSize(300,210);
addQuestionDialog.setVisible(true);
addQuestionDialog.setLocation(x, y);
addQuestionDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
addQuestionPane.addPropertyChangeListener(this);
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (addQuestionDialog.isVisible() && (e.getSource() == addQuestionPane) && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
Object value = addQuestionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
//Reset the JOptionPane's value.
//If you don't do this, then if the user
//presses the same button next time, no
//property change event will be fired.
addQuestionPane.setValue(
JOptionPane.UNINITIALIZED_VALUE);
if (value.equals("Enter")) {
String questionTypedText = questionTextField.getText();
String mqlYesTypedText = mqlYesTextField.getText();
String mqlNoTypedText = mqlNoTextField.getText();
sqlModel.addQuestion(questionTypedText, mqlYesTypedText, mqlNoTypedText);
questionTextField.setText("");
mqlYesTextField.setText("");
mqlNoTextField.setText("");
} else { //user closed dialog or clicked cancel
addQuestionDialog.setVisible(false);
}
}
}
我检查了代码好几次,没有发现任何问题,而且对话框按照预期的方式工作,所以我猜有一个特殊的 addQuestion.set。 ..(Object setValue) 我应该添加的方法。
Would I need to set a certain property to the JDialog to make sure it stays even when I switch windows.
是的。
I haven't assigned a parent for the JDialog in it's constructor,
那就是问题所在。只要对话框的所有者可见,对话框就会可见,因此您需要指定所有者 JFrame。
我正在尝试制作一个简单的 JDialog,它要求用户以 3 个文本字段的形式输入,并且它显示正确并且它的 PropertyListener 工作得很好,我还没有为 JDialog 分配父级在它的构造函数中,所以我猜测默认情况下父级设置为我的小程序中所有组件的祖先。但是,当我从 applet 更改为 firefox window 并且当我返回我的 applet 时,JDialog 消失了。我是否需要为 JDialog 设置某个 属性 以确保即使在我切换 windows 时它仍然存在。令人震惊的是,我认为该对话框仍在运行,但不可见,因为当第一个对话框消失后另一个对话框出现时,两个对话框同时出现(第一个对话框重新出现)。我的 JDialog 代码如下:
private void addQuestion() {
questionTextField = new TextField(50);
Object[] componentsArray = {"Question:", questionTextField, "MQLYes:", mqlYesTextField, "MQLNo:", mqlNoTextField};
Object[] options = {"Enter", "Cancel"};
addQuestionDialog = new JDialog(new JFrame(),"Add question");
addQuestionPane = new JOptionPane(componentsArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[0]);
int x = getX() + getWidth()/2, y = getY() + getHeight()/2;
addQuestionDialog.setContentPane(addQuestionPane);
addQuestionDialog.setResizable(false);
addQuestionDialog.setSize(300,210);
addQuestionDialog.setVisible(true);
addQuestionDialog.setLocation(x, y);
addQuestionDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
addQuestionPane.addPropertyChangeListener(this);
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (addQuestionDialog.isVisible() && (e.getSource() == addQuestionPane) && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
Object value = addQuestionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
//Reset the JOptionPane's value.
//If you don't do this, then if the user
//presses the same button next time, no
//property change event will be fired.
addQuestionPane.setValue(
JOptionPane.UNINITIALIZED_VALUE);
if (value.equals("Enter")) {
String questionTypedText = questionTextField.getText();
String mqlYesTypedText = mqlYesTextField.getText();
String mqlNoTypedText = mqlNoTextField.getText();
sqlModel.addQuestion(questionTypedText, mqlYesTypedText, mqlNoTypedText);
questionTextField.setText("");
mqlYesTextField.setText("");
mqlNoTextField.setText("");
} else { //user closed dialog or clicked cancel
addQuestionDialog.setVisible(false);
}
}
}
我检查了代码好几次,没有发现任何问题,而且对话框按照预期的方式工作,所以我猜有一个特殊的 addQuestion.set。 ..(Object setValue) 我应该添加的方法。
Would I need to set a certain property to the JDialog to make sure it stays even when I switch windows.
是的。
I haven't assigned a parent for the JDialog in it's constructor,
那就是问题所在。只要对话框的所有者可见,对话框就会可见,因此您需要指定所有者 JFrame。