JPanel with background image, with other panels overlayed

醉酒当歌 提交于 2019-12-02 00:10:28

问题


I want to have a JPanel which uses an image as a background, with this I want to add new panels to this panels so that they sit on top of this background image. I have tried the following:

Image background;
 public Table(){
  super();
   ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTable.png"));
      background = ii.getImage();
      setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);
 }
 @Override
 protected void paintComponent(Graphics g)
 {
  super.paintComponent(g); 
  if (background != null){
        g.drawImage(background, 0,0,this.getWidth(),this.getHeight(),this);
  }

      JButton button = new JButton("hello world");

      JPanel OverlayedPanel1 = new JPanel();
      OverlayedPanel1.setMinimumSize(new Dimension(600,50));
      OverlayedPanel1.setMaximumSize(new Dimension(600,50));
      OverlayedPanel1.setPreferredSize(new Dimension(600,50));
         OverlayedPanel1.add(button, BorderLayout.CENTER);
      OverlayedPanel1.setBackground(Color.yellow);

  }

The background image is displayed but the OverlayedPanel1 doesnt show. Any ideas?


回答1:


You didn't add OverlayedPanel1 to the panel.

add(OverlayedPanel1);


来源:https://stackoverflow.com/questions/2141020/jpanel-with-background-image-with-other-panels-overlayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!