将JInternalFrame添加到fsem框架

时间:2013-04-01 02:08:51

标签: java swing fullscreen paintcomponent jinternalframe

我正在尝试将jinternal帧添加到处于全屏独占模式的帧中。但是当我添加它时,它会用白色填满整个屏幕。这是我的代码。

    JFrame f = new JFrame();
    f.setTitle("Platform Game"); 
    f.setLocationRelativeTo(null); 
    f.setUndecorated(true);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    gs.setFullScreenWindow(f);
    f.setVisible(true);
    createFrame(f);


protected static void createFrame(JFrame f)
{
    JInternalFrame frame = new JInternalFrame("Options", false, true, false, false);
    frame.setVisible(true);
    frame.setSize(10, 10);
    frame.setLocation(10, 10);
    JDesktopPane pane = new JDesktopPane();
    pane.add(frame);
    f.setContentPane(pane);

    try 
    {
        frame.setSelected(true);
    } 
    catch (java.beans.PropertyVetoException e) 
    {
        e.printStackTrace();
    }
}

如果有人知道怎么做,请发布,或告诉我我做错了什么。

详细

操作系统:Windows 7

JDK:jdk 1.7.0_11

IDE:eclipse

我在jframe上使用面板,我正在使用面板

绘画代码

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    for(int i = 0; i<1000; i++)
        g.drawImage(bi, i*bi.getWidth()-getXs(), 0, bi.getWidth(), Main.HEIGHT, null);

    g.setColor(Color.WHITE);

    for(int x = 0; x<Main.X_TILES; x++)
    {
        for(int y = 0; y<Main.Y_TILES; y++)
        {
            Block block = map[x][y];

            if(block!=null && x*Block.WIDTH>=xs-Block.WIDTH && x*Block.WIDTH<=xs+Main.WIDTH)
                g.drawImage(block.getImage(x,y), x*Block.WIDTH-getXs(), y*Block.HEIGHT, Block.WIDTH, Block.HEIGHT, null);
        }
    }

    for(Entity entity : entities)
    {
        if(entity!=null && entity.getX()>=xs-Block.WIDTH && entity.getX()<=xs+Main.WIDTH)
            g.drawImage(entity.getImage(), entity.getX()-getXs(), entity.getY(), entity.getWidth(), entity.getHeight(), null);
    }

    if(displayDebug)
    {
        g.drawString("Free memory "+Runtime.getRuntime().freeMemory()/1024+" KB", 10, 12);
        g.drawString("Total memory "+Runtime.getRuntime().totalMemory()/1024+" KB", 10, 24);
        g.drawString("Max memory "+Runtime.getRuntime().maxMemory()/1024+" KB", 10, 36);
        g.drawString("X "+character.getX(), 10, 48);
        g.drawString("Y "+character.getY(), 10, 60);
        g.drawString("XS "+xs, 10, 72);
    }

    g.setColor(Color.BLACK);
    g.drawString("Life "+character.getHealth(), 700, 12);
    g.dispose();
}

我看到一个类似问题JInternalFrame in full-screen mode的答案,该说法直接将其放在面板上,但我尝试过但它仍然没有用。

编辑:当我评论出我的绘画代码时,我将它直接添加到面板而不是它有效,但它不会拖延,所以我的新问题是如何在绘制工作时使它出现完成。他们是否可以使用绘画组件绘制组件?

为了澄清它在没有绘画代码的情况下添加到面板时仍然无法正常工作。

1 个答案:

答案 0 :(得分:1)

您说您在框架中添加了JPanel(第二个代码段),但实际上您正在使用此行中的JDesktopPane替换框架的全部内容:

f.setContentPane(pane);

所以,除非你在Frame的玻璃窗格上或某个奇怪的地方某处添加你的游戏面板(带有重写的paintComponent方法),它将永远不会显示,因为JDesktopPane(以及你添加的JInternalFrame) it)是此时框架上唯一的组件。

另外,为了完整起见,您应该将setVisible(true)调用移动到初始化的最后(即createFrame(f);之后),并在paintComponent方法中完全删除对dispose()的调用({{ 1}})。

至少这是我想象的,尝试发布一个更简洁,自包含的示例,展示您遇到的问题,如果这没有帮助。

这是一个修改过的示例,它将在同一个全屏组件中显示自定义图形(本例中只是文本)和JInternalFrame:

g.dispose();