2016-03-13 41 views
1

这是我的代码,当我将JPanel添加到JLayeredPane时,它无法显示任何图像。 我不知道如何来显示多个JPanel的,当我需要把它们放在我的屏幕JLayeredPane中的多个JPanels绘图图像

public class game extends JFrame { 
     private JLayeredPane layeredPane; 
     private GraphicPanel gui; 
     private gameWindows test; 

      public game(){ 


      this.setTitle("gameVer0.01"); 
      this.setUndecorated(true); 
      setLocationRelativeTo(this); 
      gui=new GraphicPanel("url"); 
      test=new gameWindows("url"); 
      this.setLayout(new BorderLayout());  
     layeredPane =new JLayeredPane(); 
     layeredPane.setPreferredSize(new Dimension(1280, 720)); 


     layeredPane.add(test, 100); 
     layeredPane.add(gui, 200); 
     layeredPane.setOpaque(true); 
     layeredPane.setVisible(true); 
     this.add(layeredPane,BorderLayout.CENTER); 
      setResizable(false); 
      setExtendedState(JFrame.MAXIMIZED_BOTH); 
      final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
      device.setFullScreenWindow(this); 
      device.setDisplayMode(new DisplayMode(1280,720,32,60)); 
      this.setSize(400,400); 
      this.pack(); 
      this.setVisible(true); 


     } 
     public static void main(String srg[]){ 

       game window=new game(); 

       new BasicListener(window); 

       } 

    } 
    class gameWindows extends JPanel { 
     public gameWindows(String url) 
      { 
      setPreferredSize(new Dimension(1280,720)); 
      this.setVisible(true); 
      this.setBackground(Color.blue); 
      this.setOpaque(false); 
      } 
     Image image1,image2; 
      public void paintComponent(Graphics g) { 
       /* Call the original implementation of this method */ 
       super.paintComponent(g); 
      try { 
        FileInputStream fi = new FileInputStream("C:\\Users\\casper\\Desktop\\123\\game test\\src\\data\\res\\background.png"); 
        image1 = ImageIO.read(fi); 
        fi.close(); 
        } 
        catch (Exception ex) { 
         System.out.println("No example.jpg!!"); 
        } 
       g.drawImage(image1, 0,0, null); 

       } 

    } 
    class GraphicPanel extends JPanel 
    { 
     public GraphicPanel(String url) 
     { 
     this.setPreferredSize(new Dimension(800,600)); 
     this.setVisible(true); 
     this.setBackground(Color.blue); 
     this.setOpaque(false); 

     } 
     @Override 
     public void paintComponent(Graphics g) { 
      /* Call the original implementation of this method */ 
      super.paintComponent(g); 
     try { 
       FileInputStream fi = new FileInputStream("C:\\Users\\casper\\Desktop\\123\\game test\\src\\data\\res\\1.jpg");  
       image = ImageIO.read(fi); 
       fi.close(); 
       } 
       catch (Exception ex) { 
        System.out.println("No example.jpg!!"); 
       } 

       g.drawImage(image, 0, 0, null); 
     } 
    } 

这个问题上困扰我

感谢日对所有受访者

回答

1

一个JLayeredPane的使用空布局。因此,您负责设置添加到分层窗格的任何组件的大小和位置。否则默认大小是(0,0),所以没有东西可以绘制。

阅读Swing教程How to Use Layered Panes中的部分以获取工作示例。下载演示代码并进行测试,并确保您了解它。然后你可以修复你的代码。

+0

Thx,我现在修复我的代码。 – casper