java – 如何将JFrame设置为JDialog的父级

我无法将框架设置为对话框的所有者.通常当我扩展jdialog类来创建对话框时,我使用超级(框架)来指定对话框的所有者,以便当您按Alt选项卡时,它们都不会脱节.但是当我使用新的jdialog对话框= new jdialog()创建一个对话框时,我无法将框架指定为对话框的所有者.

以上示例以上两种方法.顶部点击按钮打开一个没有扩展jdialog的对话框. Bottom 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);
        }
    }
}

解决方法

对话框(或窗口)的所有者只能在构造函数中设置,因此设置它的唯一方法是使用将所有者作为参数的构造函数,如:
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);
    }
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...