单击菜单栏时,全屏独占模式崩溃

时间:2015-05-16 21:47:37

标签: java swing jframe fullscreen jmenu

我正在尝试使用全屏独占模式来编写用Java编写的游戏。我成功地让游戏以全屏独占模式运行。但是,我遇到的问题是:当我点击主框架顶部的菜单栏时,屏幕变黑;似乎一切都崩溃了。

我不确定是什么问题因为我遵循了Oracle提供的说明:https://docs.oracle.com/javase/tutorial/extra/fullscreen/

下面的第一个代码段是程序启动的位置,并检查是否支持全屏独占模式。如果支持,则进入模式。

public static void main(String[] args)
{
    GraphicsEnvironment graphhicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = graphhicsEnv.getDefaultScreenDevice();

    GraphicsConfiguration graphicsConfiguration = device.getDefaultConfiguration();

    final Frame frame = new Frame(graphicsConfiguration);

    if (device.isFullScreenSupported())
    {
        try
        {   
            // Turn on Full-Screen Exclusive Mode on the main frame.
            device.setFullScreenWindow(frame);

            frame.createBufferStrategy(2);
            BufferStrategy bufferStrategy = frame.getBufferStrategy();

            frame.requestFocus();

            while (true)
            {
                Graphics g = bufferStrategy.getDrawGraphics();

                frame.paint(g);

                g.dispose();

                bufferStrategy.show();
            }
        }
        catch (Exception e)
        { e.printStackTrace(); }
        finally
        { device.setFullScreenWindow(null); }
    }

}

下面的代码段是Frame类的构造函数,它实现了程序的主框架:

public class Frame extends JFrame
{
// The content panel of the main frame.
private Panel panel;



/*************************************
 * Default constructor of class Frame.
 *************************************/
public Frame (GraphicsConfiguration graphicsConfiguration)
{
    super("Chaos", graphicsConfiguration);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setResizable(false);

    this.setIgnoreRepaint(true);

    this.setUndecorated(true);

    // Create the content panel for this main frame.
    this.panel = new OceanPanel();

    // Add the content panel just created to the main frame.
    this.setContentPane(this.panel);

    // Set the menu bar.
    this.setMenuBar();
}

最后,下面的代码段是方法setMenuBar()的实现,它添加了所有菜单项。

private void setMenuBar()
{
    // Create a the menu bar.
    JMenuBar menuBar = new JMenuBar();

    // Create the menus.
    JMenu menu   = new JMenu("Menu");
    JMenu weapon = new JMenu("Weapon");

    // Create the menu items for the menu "Stuff".
    JMenuItem waves = new JMenuItem("Ocean Waves");
    JMenuItem quit  = new JMenuItem("Quit");

    ButtonGroup group = new ButtonGroup();

    // Create menu items for the menu "Weapon".
    final JRadioButtonMenuItem none   = new JRadioButtonMenuItem("None");
    final JRadioButtonMenuItem pistol = new JRadioButtonMenuItem("Pistol");



    /********************************************************
     * Add action listener for each item in the menu "Stuff".
     ********************************************************/

    // 
    waves.addActionListener(new ActionListener()
                            {
                                @Override
                                public void actionPerformed(ActionEvent arg0)
                                {
                                    // Toggle the ocean waves on and off.
                                    //panel.toggleWaves();
                                }
                            });

    // 
    quit.addActionListener(new ActionListener()
                           {
                                @Override
                                public void actionPerformed(ActionEvent arg0)
                                {
                                    setVisible(false);
                                    dispose();
                                    System.exit(0);
                                }
                           });



    /*********************************************************
     * Add action listener for each item in the menu "Weapon".
     *********************************************************/

    // "none" menu item hides the weapon.
    none.addActionListener(new ActionListener()
                           {
                                @Override
                                public void actionPerformed(ActionEvent arg0)
                                {
                                    panel.setWeapon(Panel.WEAPON_TYPE.NONE);
                                }
                           });


    pistol.addActionListener(new ActionListener()
                           {
                                @Override
                                public void actionPerformed(ActionEvent arg0)
                                {
                                    panel.setWeapon(Panel.WEAPON_TYPE.PISTOL);
                                }
                           });


    // ------------------------
    // Add everything together.
    // ------------------------
    menu.add(waves);
    menu.add(quit);

    group.add(none);
    group.add(pistol);

    weapon.add(none);
    weapon.add(pistol);

    menuBar.add(menu);
    menuBar.add(weapon);

    // Add the menu bar just created into the main frame.
    this.setJMenuBar(menuBar);
}

该程序可以在全屏独占模式下运行。但是,只要我点击菜单栏,一切都会崩溃,屏幕就会变黑。我无法收回对该计划的控制权;键盘上什么也没有用。我不得不按住电源按钮强制关闭计算机。

我正在使用MacBook Pro Mid 2012并使用Java 1.8版。

0 个答案:

没有答案