GrahicsDevice和JOptionPane问题 [英] GrahicsDevice and JOptionPane Issue

查看:111
本文介绍了GrahicsDevice和JOptionPane问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮时,设置为全屏的应用程序将转到任务栏/最小化,因此我需要先在任务栏中单击它,然后才能看到我触发的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. What do you think is the problem with this? I'd like it to run smoothly without being minimized or going to taskbar.

public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setTitle("Sample");
        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("Btn");
        JPanel panel = new JPanel();

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

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Sample");
                throw new UnsupportedOperationException("Not supported yet.");
            }

        });
    }

推荐答案

我能想到的唯一解决方法是将WindowAdpater添加到JFrame中,它将覆盖windowIconified(..).还有一个boolean用作程序的标志,以了解由于显示了JOptionPane窗口何时被图标化.

The only work around I can think of is to add a WindowAdpater to JFrame which will override windowIconified(..). There is also a boolean used as a flag for the program to know when the window is iconified due to JOptionPane being shown.

它确实很hacky,只有在几次屏幕闪烁后,我们才能看到JOptionPaneJFrame很好地协同工作.

Its really hacky though and only after a few screen flashes do we see the JOptionPane and JFrame working together nicely.

这是代码:

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

public class Test {

    private static boolean programmatic = false;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setTitle("Sample");
                GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                device.setFullScreenWindow(frame);
                device.setDisplayMode(new DisplayMode(800, 600, 32, 60));

                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowIconified(WindowEvent we) {
                        //super.windowIconified(we);
                        if (programmatic) {
                            programmatic = false;
                            frame.setState(JFrame.NORMAL);
                        }
                    }
                });

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

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

                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        programmatic = true;
                        JOptionPane.showMessageDialog(panel, "Sample");
                    }
                });
                frame.setVisible(true);
            }
        });
    }
}

再想一想,JDialog也会重现结果,我认为这是由于JOptionPane s和JDialog s的="nofollow">模式.也许使用JDialog并设置模态即可解决问题.

And thinking about it more, JDialog also reproduces results, I think its due to the modality of JOptionPanes and JDialogs. Maybe using JDialog and setting the modality would do the trick.

这篇关于GrahicsDevice和JOptionPane问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆