jpanels、jframes和windows,噢,天哪!

dxpyg8gm  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(242)

简单地说,我正在努力使一个游戏,我在全屏工作。
我尝试使用以下代码:

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类型的东西?

edit:以下方法更改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()s)之后可能会有一些冗余或不必要的调用,任何清理建议都将不胜感激。。。
而且,块错误看起来就是这样。我忽略了一个错误。错误只发生一次,当安装程序忽略错误的第一个示例时,这可以正常工作。。。对于任何感兴趣的人,我可以张贴额外的信息,如果有人想看看,如果该块可以删除,但我不担心。。。我以后再查。

jbose2ul

jbose2ul1#

我想我得到了你需要的。
设置框架未装饰,所以它来没有任何标题栏和东西。
将面板添加到框架中,这样看起来只有面板显示。
最大化你的框架。所以现在看起来只有你的面板可以全屏显示,而没有窗口之类的东西。
框架。未装饰(真);frame.add(面板)//现在最大化你的框架。
注意:重要的是要注意,未修饰的api只能在框架不可显示时调用,因此如果它已经显示,那么首先需要执行setvisible(false)。
edit1:如果您只想获得包含面板的窗口,则可以执行以下操作:

Window win = SwingUtilities.getAncestorOfClass(Window.class, myPanel);

一旦你得到了window示例,你就可以把它传递到任何你想要的地方。
编辑2:也是 Frame 类扩展 Window 所以你可以直接做 gs.setFullScreen(frame) . 您不需要为该框架创建新窗口。

k3fezbri

k3fezbri2#

我对jpanel的理解是它是一个窗口
你为什么这么想?你看过api了吗?jpanel从窗口延伸吗?
您可以尝试使用swingutilities类。它有一个返回给定组件窗口的方法。

8gsdolmq

8gsdolmq3#

你在这个问题上有什么进展吗?如果您可以用预期的行为和代码实际执行的操作更新您的问题,这可能会很有帮助?正如已经指出的,jframe是window的一个子类,所以如果有jframe,就不需要window。
值得一提的是,我有一个在全屏模式下工作的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插入到 container 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();

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

cidc1ykv

cidc1ykv4#

jpanel不是window的子类。jframe是。
所以你可以试试:

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

appyYourFullScreenCodeFor( yourFrame );

这应该管用。

相关问题