GrahicsDevice and JOptionPane Issue

Deadly 提交于 2019-12-24 06:07:42

问题


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

        });
    }

回答1:


One workaround here would be to use

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

instead of

device.setFullScreenWindow(frame);

Also as mentioned in the comments, setVisible(true) should appear when all the components have been added.




回答2:


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.

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

Here is the code:

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

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.



来源:https://stackoverflow.com/questions/14014523/grahicsdevice-and-joptionpane-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!