在多个模态对话框之间切换焦点

时间:2011-09-16 22:47:48

标签: java swing

是否可以在两个模态对话框之间切换焦点?也就是说,一个对话框具有焦点并且是交互式的,我将焦点/交互性切换到另一个对话框。这是我的示例代码:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new FlowLayout());
    frame.add(panel);
    frame.setVisible(true);
    final JDialog dialog1 = new JDialog(frame, true);
    dialog1.setContentPane(new JLabel("dialog 1"));
    dialog1.pack();
    dialog1.setLocationRelativeTo(frame);
    new Thread(new Runnable() {
        public void run() {
            dialog1.setVisible(true);
        }
    }).start();
    final JDialog dialog2 = new JDialog(frame, true);
    JButton btn = new JButton("Test");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // TODO
        }
    });
    dialog2.setContentPane(btn);
    dialog2.pack();
    dialog2.setLocationRelativeTo(frame);
    new Thread(new Runnable() {
        public void run() {
            dialog2.setVisible(true);
        }
    }).start();
}

我可以把任何东西都放在TODO上,让对话框成为焦点吗?我已经尝试了toFrontsetAlwaysOnTop(true)grabFocus,但没有人能够做到这一点。他们会将dialog1带到前面,但dialog2仍然是交互式的。

谢谢,

大卫

0 个答案:

没有答案