简体   繁体   English

是否可以使用 Java 中的 JOptionPane 同时显示多个弹出窗口?

[英]Is it possible to have multiple pop-ups present at the same time using the JOptionPane in java?

I want to create a program that displays multiple pop ups at the same time in different locations.我想创建一个在不同位置同时显示多个弹出窗口的程序。

This program creates on pop up shifted to the right and one jFrame shifted to the left.该程序创建向右移动的弹出窗口和向左移动的一个 jFrame。

I want both frames to be pop ups.我希望两个框架都是弹出窗口。

Here is my code:这是我的代码:

import javax.swing.*;
import java.awt.*;
public class jFrame{
    public static void createWindow(){

        JFrame frame = new JFrame();
        JFrame frame2 = new JFrame();

        frame.setVisible(true);
        frame.setLocation(100,500);

        frame2.setVisible(true);
        frame2.setLocation(900,500);

        int x = 0;
        int y = 5;
        while(y>x){
          int reply = JOptionPane.showConfirmDialog(frame, "Do you wanna get rid of this popup?", "Message", JOptionPane.YES_NO_OPTION);
          if (reply == JOptionPane.YES_OPTION) {
            JOptionPane.showConfirmDialog(frame, "Sike \n You thought", "Message", JOptionPane.OK_CANCEL_OPTION);
          }      
          int reply2 = JOptionPane.showConfirmDialog(frame2, "Do you wanna get rid of this popup?", "Message", JOptionPane.YES_NO_OPTION);
          if (reply2 == JOptionPane.YES_OPTION) {
            JOptionPane.showConfirmDialog(frame2, "Sike \n You thought", "Message", JOptionPane.OK_CANCEL_OPTION);
          }
        }
    }  

    public static void main(String[] args){
        createWindow()
    }
}

Start by taking a look at How to Use Modality in Dialogs首先看看如何在对话框中使用模态

Basically, the default modality of the dialog (when setModal is true ) is to block all top level containers.基本上,对话框的默认模式(当setModaltrue )是阻止所有顶级容器。 You can control this by chaning the ModalityType of the dialog, for example您可以通过更改对话框的ModalityType来控制它,例如

对话框

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DialogTest {

    public static void main(String[] args) {
        new DialogTest();
    }

    public DialogTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice device = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = device.getDefaultConfiguration();
                Rectangle bounds = gc.getBounds();
                Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);

                bounds.x += insets.left;
                bounds.y += insets.top;
                bounds.width -= (insets.left + insets.right);
                bounds.height -= (insets.top + insets.bottom);


                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocation(bounds.x + ((bounds.width / 2) - frame.getWidth()),
                        bounds.y + ((bounds.height / 2) - frame.getHeight()));
                frame.setVisible(true);

                JFrame frame2 = new JFrame("Testing");
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame2.add(new TestPane());
                frame2.pack();
                frame2.setLocation(bounds.x + ((bounds.width / 2)),
                        bounds.y + ((bounds.height / 2) - frame2.getHeight()));
                frame2.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JButton popup = new JButton("Popup");
            add(popup);

            popup.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JDialog dialog = new JDialog(SwingUtilities.windowForComponent(TestPane.this));
                    dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);

                    JPanel panel = new JPanel(new GridBagLayout());
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridwidth = GridBagConstraints.REMAINDER;
                    panel.add(new JLabel("I'll be your dialog today"), gbc);
                    JButton close = new JButton("Close");
                    close.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            SwingUtilities.windowForComponent(close).dispose();
                        }
                    });
                    panel.add(close, gbc);
                    dialog.add(panel);
                    dialog.pack();
                    dialog.setLocationRelativeTo(TestPane.this);
                    dialog.setVisible(true);
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

Now, before you ask about getting JOptionPane to work this way, the answer is basically, no.现在,在您询问让JOptionPane以这种方式工作之前,答案基本上是否定的。

You "can" make a custom extension to JOptionPane , which overrides the two createDialog methods, but you won't be able to use the static helper methods, as they create an instance of JOptionPane您“可以”对JOptionPane进行自定义扩展,该扩展覆盖了两个createDialog方法,但您将无法使用static帮助器方法,因为它们创建了JOptionPane的实例

yes Of Course It is, the only way is :是的 当然是的,唯一的方法是:

lets imagine that we have One Panel : myPanel让我们想象一下我们有一个面板:myPanel

in myPanel we have one button btn1在 myPanel 中,我们有一个按钮 btn1

in btn1's eventListner we have a method that display a JOptionPan pop1 where we have an another button btn2在 btn1 的 eventListner 中,我们有一个显示 JOptionPan pop1 的方法,其中我们有另一个按钮 btn2

we can have inside btn2's eventListner that display also an other JOptionPan pop2.我们可以在 btn2 的 eventListner 中显示另一个 JOptionPan pop2。

So it is possible.所以这是可能的。 And you can add customized JOptionPans By the way.顺便说一下,您可以添加自定义的 JOptionPans。

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

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