1

当我单击按钮时,设置为全屏的应用程序将转到任务栏/最小化,因此我需要先在任务栏中单击它,然后才能看到JOptionPane我触发的。你认为这有什么问题?我希望它能够顺利运行,而不会被最小化或进入任务栏。

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

        });
    }
4

2 回答 2

1

我能想到的唯一解决方法是添加一个WindowAdpaterJFrame覆盖的windowIconified(..)。还有一个boolean用作程序标志,用于知道窗口何时因JOptionPane显示而被图标化。

虽然它真的很hacky,只有在几次屏幕闪烁之后,我们才能看到它们很好地协同工作JOptionPaneJFrame

这是代码:

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也再现了结果,我想是因为s和s的模态。也许使用和设置模态就可以了。JOptionPaneJDialogJDialog

于 2012-12-23T21:30:58.880 回答
1

这里的一种解决方法是使用

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

代替

device.setFullScreenWindow(frame);

同样如评论中所述,setVisible(true)应在添加所有组件后出现。

于 2012-12-23T21:09:32.697 回答