Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 JPanels、JFrames和Windows,噢,天哪!_Java_Window_Jframe_Fullscreen_Jpanel - Fatal编程技术网

Java JPanels、JFrames和Windows,噢,天哪!

Java JPanels、JFrames和Windows,噢,天哪!,java,window,jframe,fullscreen,jpanel,Java,Window,Jframe,Fullscreen,Jpanel,简单地说,我正在尝试制作一个全屏游戏 我尝试使用以下代码: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); if(!gs.isFullScreenSupported()) { System.out.println("full-screen not supported"); } Frame

简单地说,我正在尝试制作一个全屏游戏

我尝试使用以下代码:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if(!gs.isFullScreenSupported()) {
    System.out.println("full-screen not supported");
}

Frame frame = new Frame(gs.getDefaultConfiguration());
Window win = new Window(frame);

try {
    // Enter full-screen mode
    gs.setFullScreenWindow(win);
    win.validate();
}
问题是我在一个扩展JPanel的类中工作,虽然我有一个Frame类型的变量,但在类中没有Window类型的变量

我对JPanel的理解是,它是一个窗口,但我不能将“this”传递给gs.setFullScreenWindow(windowwin)。。。我该怎么做呢

有没有什么简单的方法可以使用JPanel调用它或类似的方法

有没有办法从我的JPanel中获取Window类型的东西

-

编辑:以下方法更改JFrame的状态,并每10ms调用一次:

public void paintScreen()
{
    Graphics g;
    try{
        g = this.getGraphics(); //get Panel's graphic context
        if(g == null)
        {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
            frame.add(this);
            frame.pack();
            frame.setResizable(false);
            frame.setTitle("Game Window");
            frame.setVisible(true);
        }
        if((g != null) && (dbImage != null))
        {
            g.drawImage(dbImage, 0, 0, null);
        }
        Toolkit.getDefaultToolkit().sync(); //sync the display on some systems
        g.dispose();
    }
    catch (Exception e)
    {
        if(blockError)
        {
            blockError = false;
        }
        else
        {
            System.out.println("Graphics context error: " + e);
        }
    }
}
我预计在if(g==null)语句(所有frame.somethingOrOther()语句)之后可能会有一些冗余或不必要的调用,任何清理建议都将不胜感激

此外,块错误看起来也是如此。我忽略了一个错误。该错误只发生一次,当安装程序忽略该错误的第一个实例时,该操作正常。。。对于任何感兴趣的人,如果有人想知道是否可以删除该块,我可以在那里发布其他信息,但我不担心。。。我可能以后再调查

我对JPanel的理解是 这是一扇窗户

你为什么这么想?你看过API了吗?JPanel是否从窗口延伸

您可以尝试使用SwingUtilities类。它有一个返回给定组件窗口的方法。

不是的子类。是

所以你可以试试:

JFrame yourFrame = new JFrame();
yourFrame.add(yourPanel);

appyYourFullScreenCodeFor( yourFrame );

那应该行。

我想我得到了你需要的东西

  • 将框架设置为未装饰,以便 没有任何标题栏和 东西

  • 将面板添加到框架中,以便 看起来只有您的面板显示

  • 最大化你的框架。所以现在它 应该看起来只有你的 全屏显示的面板,无需 还有窗户的东西

    框架。设置未装饰(真实); 框架。添加(面板)//现在最大化你的 框架

  • 注意:需要注意的是,未修饰的API只能在您的帧不可显示时调用,因此如果它已经显示,那么首先需要执行setVisible(false)

    EDIT1:如果您只想获得包含面板的窗口,则可以执行以下操作:

    Window win = SwingUtilities.getAncestorOfClass(Window.class, myPanel);
    
    一旦获得窗口实例,就可以将其传递到任何需要的地方


    EDIT2:同时
    Frame
    类扩展了
    窗口
    ,因此您可以直接执行
    gs.setFullScreen(Frame)
    。您不需要为该框架创建新窗口。

    您在这个问题上取得了任何进展吗?如果您可以使用预期的行为和代码实际执行的操作更新您的问题,这可能会有所帮助?正如已经指出的,JFrame是Window的一个子类,所以如果有JFrame,就不需要窗口

    值得一提的是,我有一个在全屏模式下工作的Java应用程序。虽然屏幕的重新绘制频率不如您的屏幕,但它会定期重新绘制。我执行以下操作进入全屏:

    // pseudo-code; not compilable
    
    JPanel container = new JPanel();
    container.setOpaque( true ); // make sure the container will be visible
    
    JFrame frame = new JFrame();
    frame.getContentPane().add(container); // add the container to the frame
    frame. ... //other initialization stuff, like default close operation, maximize, etc
    
    if ( fullScreenModeIsSupported )
        frame.setUndecorated( true ); // remove window decorations from the frame
        gs.setFullScreenWindow( frame );
        frame.validate();
    
    然后,每当我需要更新屏幕时,我只需将一个新的JPanel插入
    容器
    JPanel:

    // pseudo-code; not compilable
    
    container.removeAll(); // clean out the container
    container.add( jPanelWithNewDisplay ); // add the new display components to the container
    container.validate();  // update and redisplay
    container.repaint();
    

    我不能说它在技术上是完美的,但对我来说效果很好。如果伪代码示例不能解决这个问题,我可以花一些时间编写一个可编译的示例。

    我认为这不是我需要的。我相信我上面提到的代码利用了您的解决方案所没有的几种效率。最值得注意的是,如果我是正确的,它不会像在窗口或模拟全屏(您的解决方案)中一样工作。它要么在所有其他窗口和任务栏上最大化,要么在任务栏上最小化。这样,它就不必担心检查顶部的其他窗口和调用重绘方法。它看起来也更干净。如果你运行上面的代码,你就会明白我的意思。@Jonathan…如果有帮助,请参阅我对答案的编辑。如果您错过了,我会在评论中发布:如果您只想获得包含面板的窗口,那么您可以这样做:window win=SwingUtilities.getAncestorOfClass(window.class,myPanel);一旦你得到了窗口实例,你就可以把它传递到任何你想传递的地方。我的错误。这似乎不起作用,但我认为这可能是我在原始问题中附加的方法的一部分。该方法每10毫秒调用一次,并与我的JFrame的状态发生冲突。