如何将JFrame设置为JDialog的父级窗口

15

我在设置框架作为对话框的所有者时遇到了麻烦。通常当我扩展JDialog类来创建对话框时,我使用super(frame)来指定对话框的所有者,以便在按alt + tab时它们两个不是不相交的。但是,当我使用new创建对话框时,例如JDialog dialog = new JDialog(),我无法将框架指定为对话框的所有者。

以下示例演示了上述两种方法。Top Click按钮打开一个对话框,该对话框未扩展JDialogBottom Click按钮打开一个对话框,扩展了JDialog

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class DialogEx {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                new DialogEx().createUI();
            }
        };
        EventQueue.invokeLater(r);
    }   

    private void createUI() {
        final JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JButton button1 = new JButton("Top Click");
        JButton button2 = new JButton("Bottom Click");

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new DialogExtend(frame).createUI();
            }
        });

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                new DialogWithoutExtend(frame).cretaUI();
            }
        });

        frame.setTitle("Test Dialog Instances.");
        frame.add(button1, BorderLayout.NORTH);
        frame.add(button2, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(300, 200));
        frame.setVisible(true);
    }

    class DialogExtend extends JDialog {
        private JFrame frame;
        public DialogExtend(JFrame frame) {
            super(frame);
            this.frame = frame;
        }

        public void createUI() {
            setLocationRelativeTo(frame);
            setTitle("Dialog created by extending JDialog class.");
            setSize(new Dimension(400, 100));
            setModal(true);
            setVisible(true);
        }
    }

    class DialogWithoutExtend {

        private JFrame frame;
        public DialogWithoutExtend(JFrame frame) {
            this.frame = frame;
        }

        public void cretaUI() {
            JDialog dialog = new JDialog();
            dialog.setTitle("Dialog created without extending JDialog class.");
            dialog.setSize(new Dimension(400, 100));
            dialog.setLocationRelativeTo(frame);
            dialog.setModal(true);
            dialog.setVisible(true);
        }
    }
}

实际上查看了你的代码:除了在扩展中给frame起不必要的别名之外,还有什么问题吗? - kleopatra
@kleopatra,我已经更新了代码。请查看创建一个不继承JDialog的对话框的类DialogWithoutExtend。您能告诉我如何将该对话框的所有者设置为该框架吗? - Amarnath
还是看不到它:new JDialog(frame) 怎么了? - kleopatra
2
+1 对于 SSCCE - Reimeus
@kleopatra 我知道这是一个过时的线程 - 但为什么扩展JFrame(或JDialog)是一个如此糟糕的想法呢? - ags
显示剩余5条评论
1个回答

13

对话框(或窗口)的所有者只能在构造函数中设置,因此唯一设置它的方法是使用接受所有者作为参数的构造函数,例如:

class DialogWithoutExtend {

    private JFrame frame;
    public DialogWithoutExtend(JFrame frame) {
        this.frame = frame;
    }

    public void cretaUI() {
        JDialog dialog = new JDialog(frame);
        dialog.setTitle("Dialog created without extending JDialog class.");
        dialog.setSize(new Dimension(400, 100));
        dialog.setLocationRelativeTo(frame);
        dialog.setModal(true);
        dialog.setVisible(true);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接