加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:34:20 所属栏目:Java 来源:网络整理
导读:我无法将框架设置为对话框的所有者.通常当我扩展JDialog类来创建对话框时,我使用超级(框架)来指定对话框的所有者,以便当您按Alt选项卡时,它们都不会脱节.但是当我使用新的JDialog对话框= new 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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读