Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击菜单栏时,全屏独占模式崩溃_Java_Swing_Jframe_Fullscreen_Jmenu - Fatal编程技术网

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

Java 单击菜单栏时,全屏独占模式崩溃,java,swing,jframe,fullscreen,jmenu,Java,Swing,Jframe,Fullscreen,Jmenu,我试图使用全屏独占模式的游戏编写的Java。我成功地让游戏在全屏独占模式下运行。然而,我遇到的问题是:只要我点击主框架顶部的菜单栏,屏幕就会变黑;似乎一切都崩溃了 我不确定出了什么问题,因为我遵循了Oracle提供的说明: 下面的第一个代码段是程序启动的地方,用于检查是否支持全屏独占模式。如果支持,则进入该模式 public static void main(String[] args) { GraphicsEnvironment graphhicsEnv = GraphicsEnvir

我试图使用全屏独占模式的游戏编写的Java。我成功地让游戏在全屏独占模式下运行。然而,我遇到的问题是:只要我点击主框架顶部的菜单栏,屏幕就会变黑;似乎一切都崩溃了

我不确定出了什么问题,因为我遵循了Oracle提供的说明:

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

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);
}
该程序可以在全屏独占模式下运行。然而,当我点击菜单栏时,一切都崩溃了,屏幕变黑了。我无法收回对程序的控制权;键盘上没有任何功能。我不得不按住电源按钮来强制关闭电脑


2012年年中,我正在使用MacBook Pro,并使用Java 1.8版。

您不能将swing控件与BufferStragey一起使用,因为它们具有不同的绘图功能requirements@MadProgrammer,你能详细说明你的意见吗?您是说我不能将JMenu、JMenuBar和JMenuItem与BufferStrategy一起使用?你对这项工作有什么建议?我从来没有看到任何地方提到我们不能将swing控件与BufferStrategy一起使用。你能提供一个源代码吗?Swing使用它自己的“被动”渲染引擎,但要知道你已经用主动绘制方法控制了它,它们会发生冲突