1

我开发了用于JDialog显示弹出窗口的 Swing 应用程序。

但问题是当我按下alt+时tab,它只显示对话框而不显示应用程序。我还尝试了对话框模式。

我的要求是当在应用程序上打开对话框并按下alt+tab键时,它会切换到另一个 X 应用程序,当我再次按下alt+tab键时,它会显示在我的应用程序上打开的对话框。目前它显示打开的对话框,但不单独显示应用程序。

我怎样才能满足这个要求JDialog呢?

这是示例代码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
*$Id$
*/
public class Main
{
    public static void main(final String[] args)
    {
        final JFrame jFrame = new JFrame();
        jFrame.setSize(300, 200);
        final JPanel panel = new JPanel();
        final JButton button = new JButton("click here to open dialog");
        final ProductDialog dialog = new ProductDialog();
        button.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(final ActionEvent e)
            {
                dialog.setVisible(true);
            }
        });
        panel.add(button);
        jFrame.add(panel);
        jFrame.setVisible(true);
    }
}

对话框如下

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ProductDialog extends JDialog
{
    private static final long serialVersionUID = 1L;

    public ProductDialog()
    {
        this.add(new JPanel().add(new JLabel("Test")));
        this.setSize(150, 100);
        this.setModal(true);
        this.setLocationRelativeTo(null);
    }
}

这是一个小应用程序的视觉效果图。当前在 Windows 7上的alt+中显示安全对话框。该应用程序。tab其本身已经在屏幕上可见,尽管安全对话框(左上角)仅显示在较小的图标中。

在此处输入图像描述

4

2 回答 2

2

您需要将对话框的父窗口设置为Frame应用程序的父窗口。

小例子:

import java.awt.Window;
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.SwingUtilities;

public class TestDialog {

    protected void initUI() {
        JFrame frame = new JFrame(TestDialog.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JButton button = new JButton("Click me to open dialog");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Window parentWindow = SwingUtilities.windowForComponent(button);
                JDialog dialog = new JDialog(parentWindow);
                dialog.setLocationRelativeTo(button);
                dialog.setModal(true);
                dialog.add(new JLabel("A dialog"));
                dialog.pack();
                dialog.setVisible(true);
            }
        });
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestDialog().initUI();
            }
        });
    }
}
于 2012-12-10T13:09:11.057 回答
0

我已将父框架传递给对话框,如下所示。

final ProductDialog dialog = new ProductDialog(jFrame);

并将其设置为
以父窗口为参数调用超类的构造函数并设置对话框的模态类型,它对我有用。

import java.awt.Dialog;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ProductDialog extends JDialog
{
    private static final long serialVersionUID = 1L;

    public ProductDialog(final JFrame jFrame)
    {
        super(jFrame);
        this.add(new JPanel().add(new JLabel("Test")));
        this.setSize(150, 100);
        this.setModal(true);
        this.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
    }
}

这只是我为测试而创建的示例。

于 2012-12-11T07:30:12.930 回答