没有app的对话框可见。在alt选项卡上

时间:2012-12-10 12:47:51

标签: java swing jdialog

我开发了Swing应用程序,其中我使用JDialog来显示弹出窗口。

但问题是,当我按 alt + tab 时,它只显示对话框而非应用程序。我也试过模态对话。

我的要求是在应用程序上打开对话框并按 alt + 选项卡键时,它会切换到另一个X应用程序,当我按 alt <时/ kbd> + 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 + 选项卡中显示安全对话框。该应用程序。虽然安全对话框(左上角)显示在较小的图标中,但它本身已经在屏幕上可见。

enter image description here

2 个答案:

答案 0 :(得分: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();
            }
        });
    }
}

答案 1 :(得分: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);
    }
}

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