多个JPanels在JLayeredPane中绘制图像

时间:2016-03-13 10:24:54

标签: java swing jpanel paint jlayeredpane

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

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 个答案:

答案 0 :(得分:1)

JLayeredPane使用null布局。因此,您负责设置添加到分层窗格的任何组件的大小和位置。否则默认大小为(0,0),因此无需绘制任何内容。

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