关闭文档模式 JDialog 时,是否可以使 Java Swing 激活父 JFrame?默认机制似乎激活了最后一个 JFrame 而不是父 JFrame。这可能会使用户感到困惑。
这是一些重现问题的示例代码:
1:按下按钮“步骤 1:按下这个”
2:按下按钮“步骤 2:按下这个”
3:将 JFrame 放在前面,标题为“步骤 3:激活这个”
4:关闭标题为“的 JDialog”第 4 步:关闭此“
=> 现在,带有文本“frame 1”的 JFrame 应该放在前面。
=> 相反,带有文本“第 3 步:激活此”的 JFrame 位于前面。
=> 对于用户来说,这很令人困惑,因为关闭的 JDialog 是第 1 帧的文档模式
public static void main(String[] args) {
final JFrame f1 = new JFrame("frame 1");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setLayout(new FlowLayout());
f1.add(new JButton(new AbstractAction("step 1: press this") {
@Override
public void actionPerformed(ActionEvent e) {
JFrame f2 = new JFrame("step 3: activate this");
f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.setLayout(new FlowLayout());
f2.add(new JLabel("step 3: activate this"));
f2.pack();
f2.setVisible(true);
}
}));
f1.add(new JButton(new AbstractAction("step 2: press this") {
@Override
public void actionPerformed(ActionEvent e) {
JDialog d = new JDialog(f1, "step 4: close this", ModalityType.DOCUMENT_MODAL);
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
d.setLayout(new FlowLayout());
d.add(new JLabel("step 4: close this"));
d.pack();
d.setVisible(true);
}
}));
f1.pack();
f1.setVisible(true);
}
有任何想法吗 ?(我不想手动调用 f1.toFront(),因为会导致窗口短暂“闪烁”)
最好的问候马塞尔