简体   繁体   English

JOptionPane使用GraphicsDevice显示JFrame外部

[英]JOptionPane showing outside JFrame with GraphicsDevice

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

When I click the button, the application which is set to full screen will go to taskbar/minimized, so I need to click it first in the taskbar before seeing the JOptionPane that I triggered. 当我单击按钮时,设置为全屏的应用程序将转到任务栏/最小化,因此我需要先在任务栏中单击它,然后才能看到我触发的JOptionPane。 What do you think is the problem with this? 您认为这有什么问题? I'd like it to run smoothly without being minimized or going to taskbar. 我希望它运行平稳而不被最小化或转到任务栏。 Looking forward for your answers. 期待您的答案。 Thanks in advance. 提前致谢。 Or is there any other alternative to this? 或者还有其他替代方案吗?

That code works for me, though you might try this variant with 2 changes. 该代码对我有用,尽管您可以尝试此变体并进行2次更改。

  1. It creates and shows the GUI on the EDT. 它在EDT上创建并显示GUI。
  2. It uses the content pane of the frame as the parent of the JOptionPane 它使用框架的内容窗格作为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);
    }
}

Update 更新

When I add the following lines to the beginning of the source seen above.. 当我将以下几行添加到上面看到的源代码的开头时。

System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));

..the output & result is as follows. ..输出和结果如下。

Running in 1.7 在1.7中运行

Result: Failure as described in the question. 结果:问题中描述的失败。

1.7.0_09
23.5-b02

Running in 1.6 在1.6中运行

Result: Success with no unusual artifacts or behavior. 结果:成功,没有异常产物或行为。

1.6.0
1.6.0-b105

Analysis 分析

Note that other results from comments suggest the behavior changed some time between that early 1.6 version, and 1.6.0_25. 请注意,注释的其他结果表明该行为在早期的1.6版本和1.6.0_25之间有所变化。 It seems like a regression bug. 似乎是回归错误。 The OP should check the bug database & if nothing likely shows up, lodge a new report. OP应检查错误数据库 ,如果没有可能出现,请提交新报告。

JOptionPane.showInternalMessageDialog(frame.getContentPane(), "JOptionPane");

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

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