简体   繁体   English

Java swing中的Modal Window顶部的Modal Window?

[英]Modal Window on top of Modal Window in java swing?

I have the following problem: 我有以下问题:

I have a main application window (JFrame) that fills the whole screen. 我有一个主应用程序窗口(JFrame),它占据了整个屏幕。
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. 解决方案:打开模式JDialog。 Lets call that Dialog 1. 让我们调用该对话框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. 但是,当单击此对话框中的按钮时,应该会弹出一个新窗口(是/否对话框)..同样,还需要对已经模态化的JDialog Dialog 1进行模态化。对话框一直消失在第一个对话框的后面。
I tried making Dialog 1 a JFrame but then, of course, I loose the "modal" bit. 我尝试将Dialog 1设为JFrame,但是随后,我松开了“ modal”位。 Forcing Dialog 1 to stay in from still keeps the main window's Button clickable. 迫使对话框1停留在其中,仍使主窗口的“按钮”可单击。
What am I missing? 我想念什么? How can I put a modal swing window OVER another modal swing window? 如何将模态摆动窗口放在另一个模态摆动窗口上方?

Edit: minimal example of one not-really working version: 编辑:一个不是真正工作版本的最小示例:

Main class for opening: 开设的主要班级:

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

} }

Main window: 主视窗:

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

First Dialog: 第一个对话框:

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

Second Dialog: 第二个对话框:

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: I just realised: Dialog 2 is not hidden, as I suspected.. it is simply not there. PS:我刚刚意识到:对话框2没有隐藏,正如我所怀疑的..它根本不存在。 Most likely because the parent window is blocked from the Modal Dialog? 最有可能是因为父窗口被阻止了“模态对话框”?

Here is an MCVE of modality working as advertised. 这是如所宣传的工作方式的MCVE。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM