问题 如何将JFrame设置为JDialog的父级


我无法将框架设置为对话框的所有者。通常当我延长 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);
        }
    }
}

6870
2018-03-15 10:14


起源

实际上在你的代码中查找(没有运行):问题是什么(除了你在扩展中框架的unnessecary别名)? - kleopatra
@kleopatra 扩展JDialog和扩展JFrame一样糟糕 究竟。这就是我想知道如何在使用new创建对话框时将框架作为对话框的所有者的原因。 - Amarnath
使用构造函数,框架与扩展时相同?一定是遗漏了什么...请解释:-) - kleopatra
@kleopatra我已经更新了代码。请看课 DialogWithoutExtend  这会在不扩展JDialog的情况下创建对话框。您能告诉我如何将框架作为此对话框的所有者。 - Amarnath
+1为 SSCCE - Reimeus


答案:


可以设置对话框(或窗口)所有者 只要 在构造函数中,所以设置它的唯一方法是使用一个构造函数,它将所有者作为参数,如:

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);
    }
}

10
2018-03-15 11:00