2013-04-01 76 views
0

我正在嘗試將jinternal幀添加到全屏獨佔模式下的幀。但是,當我添加它填充整個屏幕與白色。這是我的代碼。將JInternalFrame添加到fsem幀

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:日食

我使用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

我沒有問題與得到的示例工作,除了調整大小或移動內部框架。你在 – MadProgrammer

+0

下運行什麼操作系統和JDK的Windows 7 jdk 1.7.0_11 –

+0

如果你正在做自定義繪畫,你需要提出的是你的例子以及 – MadProgrammer

回答

1

你的意思是你添加的JPanel(第2的代碼片段)到你的框架,但實際上你是在這條線與JDesktopPane的更換相框的全部內容:

f.setContentPane(pane); 

所以,除非你加入您的遊戲面板(覆蓋paintComponent方法的那個面板)在某處位於Frame的玻璃面板上或某處類似奇怪的位置,因此它將永遠不會顯示,因爲在此處的框架上的唯一組件是JDesktopPane(以及添加到它的JInternalFrame)點。

此外,對於完整性起見,您應該在調用setVisible(真)調用移動到初始化(即createFrame(f);後),完全的最後刪除調用dispose()在你的paintComponent方法(g.dispose();)。

至少這是我想象中的事情,嘗試發佈一個更簡潔和自包含的示例,展示如果這對您沒有幫助的問題。

這裏有一個修改實例,將顯示自定義圖形(只是在這種情況下,文本)和JInternalFrame的在相同的全屏幕組件:

public class FrameTest { 

    public static void main(String[] args) { 
     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); 

     JDesktopPane pane = new MyPanel(); 
     JInternalFrame frame = new JInternalFrame("Options", false, true, false, false); 
     frame.setVisible(true); 
     frame.setSize(100, 100); 
     frame.setLocation(10, 10); 
     pane.add(frame); 
     f.setContentPane(pane); 

     f.setVisible(true); 
    } 

    private static class MyPanel extends JDesktopPane { 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      g.setColor(Color.BLACK); 

      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); 
     } 
    } 
} 
+0

我試着做這個,但我不知道如何添加面板和內部框架。 –

+0

@JoshSobel例如,不是從JPanel擴展「遊戲面板」,而是從JDesktopPane擴展,以便爲自定義繪畫和JInternalFrame使用相同的組件。或者,因爲JDesktopPane擴展了JLayeredPane,所以使用其機制將自定義面板添加到比默認圖層更低的圖層。 – jkovacs

+0

虐待嘗試,明天去睡覺,現在如果它工作不正常接受你的答案 –