JPanel with background image, with other panels overlaid

I want to have a JPanel that uses the image as a background, with this I want to add new panels to the panels so that they sit on top of this background image. I 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); } 

A background image is displayed, but OverlayedPanel1 is not displayed. Any ideas?

+3
source share
1 answer

You have not added OverlayedPanel1 to the panel.

 add(OverlayedPanel1); 
+2
source

Source: https://habr.com/ru/post/1311651/


All Articles