Java swing中的Modal Window顶部的Modal Window? [英] Modal Window on top of Modal Window in java swing?

查看:140
本文介绍了Java swing中的Modal Window顶部的Modal Window?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

我有一个主应用程序窗口(JFrame),它占据了整个屏幕.
单击按钮时,会出现一个较小的窗口,让用户输入一些数据.在用户执行此操作时,主窗口不应跳到其前面,也不允许进行交互.
解决方案:打开模式JDialog.让我们调用该对话框1.
但是,当单击此对话框中的按钮时,应该会弹出一个新窗口(是/否对话框)..同样,还需要对已经模态化的JDialog Dialog 1进行模态化.对话框一直消失在第一个对话框的后面.
我尝试将Dialog 1设为JFrame,但随后,我当然松开了"modal"位.强制将Dialog 1保留下来,仍然使主窗口的Button保持可单击状态.
我想念什么?如何将模态摆动窗口置于另一个模态摆动窗口之上?

I have a main application window (JFrame) that fills the whole screen.
When a button is clicked, a smaller window appears, to let the user input some Data. While the User does this, the main window should neither jump in front of it, nor allow interaction.
Solution to that: open a modal JDialog. Lets call that Dialog 1.
But when a button within this Dialog is clicked, a NEW window (yes/no-dialog) is supposed to pop up.. and, again, needs to act modal on the already modal JDialog Dialog 1. Trying to do that, the second Dialog keeps disappearing behind the first one.
I tried making Dialog 1 a JFrame but then, of course, I loose the "modal" bit. Forcing Dialog 1 to stay in from still keeps the main window's Button clickable.
What am I missing? How can I put a modal swing window OVER another modal swing window?

一个不太实际的版本的最小示例:

minimal example of one not-really working version:

打开主类:

public class MainWin extends JFrame {

public MainWin(){
    this.setSize(800,800);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dia1(MainWin.this);
        }
    });
    this.setVisible(true);
}

}

主窗口:

 public class MainWin extends JFrame {

public MainWin(){
    this.setSize(800,800);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dia1(MainWin.this);
        }
    });
    this.setVisible(true);
}
}

第一个对话框:

public class Dia1 extends JDialog {


public Dia1(final JFrame parent){
    super(parent, true);
    this.setSize(400, 400);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    this.setVisible(true);
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dia2(parent);
        }
    });
}
}

第二个对话框:

public class Dia2 extends JDialog {


public Dia2(JFrame parent){
    super(parent, true);
    this.setSize(200, 200);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    this.setVisible(true);
}

}

PS:我刚刚意识到:对话框2没有隐藏,正如我怀疑的那样.它根本不存在.很有可能是因为父窗口被阻止了模态对话框"?

PS: I just realised: Dialog 2 is not hidden, as I suspected.. it is simply not there. Most likely because the parent window is blocked from the Modal Dialog?

推荐答案

以下是所宣传的模态MCVE.

Here is an MCVE of modality working as advertised.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ModalOverModal {

    private JComponent ui = null;

    ModalOverModal() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridLayout());
        ui.setBorder(new EmptyBorder(40,40,40,40));

        final JButton b1 = new JButton("Open Modal Dialog");
        b1.setMargin(new Insets(40, 200, 40, 200));
        ui.add(b1);

        final JButton b2 = new JButton("Open 2nd Modal Dialog");

        ActionListener al1 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(b1, b2);
            }
        };
        b1.addActionListener(al1);

        ActionListener al2 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(b2, "Close Me!");
            }
        };
        b2.addActionListener(al2);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                ModalOverModal o = new ModalOverModal();

                JFrame f = new JFrame("Modal over Modal");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于Java swing中的Modal Window顶部的Modal Window?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆