JOptionPane使用GraphicsDevice显示JFrame外部

Jon*_*ong 2 java swing awt fullscreen joptionpane

package javaapplication1;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setTitle("Frame");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        device.setFullScreenWindow(frame);
        device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
        frame.setVisible(true);

        JButton btn = new JButton();
        btn.setText("Button");
        JPanel panel = new JPanel();

        panel.add(btn);
        frame.add(panel);

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "JOptionPane");
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

当我单击按钮时,设置为全屏的应用程序将转到任务栏/最小化,因此我需要先在任务栏中单击它,然后才能看到我触发的JOptionPane.您认为这有什么问题?我希望它能够在没有最小化或进入任务栏的情况下顺利运行.期待您的回答.提前致谢.或者还有其他替代方案吗?

And*_*son 5

该代码适用于我,但您可以通过2次更改尝试此变体.

  1. 它在EDT上创建并显示GUI.
  2. 它使用框架的内容窗格作为框架的父窗格 JOptionPane

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

public class JavaApplication1 {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            public void run() {
                final JFrame frame = new JFrame();
                frame.setTitle("Frame");
                frame.setSize(800, 600);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                device.setFullScreenWindow(frame);
                device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
                frame.setVisible(true);

                JButton btn = new JButton();
                btn.setText("Button");
                JPanel panel = new JPanel();

                panel.add(btn);
                frame.add(panel);

                btn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //JOptionPane.showMessageDialog(frame, "JOptionPane");
                        JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane");
                    }
                });
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新

当我将以下行添加到上面看到的源的开头时..

System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));
Run Code Online (Sandbox Code Playgroud)

..输出和结果如下.

在1.7中运行

结果: 问题中描述的失败.

1.7.0_09
23.5-b02
Run Code Online (Sandbox Code Playgroud)

在1.6中运行

结果: 成功,没有异常的工件或行为.

1.6.0
1.6.0-b105
Run Code Online (Sandbox Code Playgroud)

分析

请注意,评论的其他结果表明,行为在早期的1.6版本和1.6.0_25之间发生了一些变化.这似乎是一个回归错误.OP应检查错误数据库,如果没有可能出现,请提交新报告.

  • @AndrewThompson正如我认为它必须是OP问题的最后答案说它有效并且他正在使用Java 6. +1来评论和回答希望你能编辑你的答案以反映你的评论,因为没有错误我可以找到已经提交的网络上的许多其他人遇到了类似的问题,这可能是解决方案/原因首次出现的地方,直到bug被提交:) (2认同)
  • @DavidKroukamp问题根据您的建议进行编辑,并提供更具体的信息.本地使用的版本. (2认同)