'Adding JPanel inside a JPanel - the nested JPanel won't display

New Code

package test;

import javax.swing.*;
import java.awt.*;


public class TestWindow extends JFrame{
//------------------------------------------------------------------------------
    public static void main(String[] args) {
        new TestWindow();
    }
//------------------------------------------------------------------------------
    public TestWindow(){
        setSize(300,300);
        this.setUndecorated(true);
        add(new Background());
        setVisible(true);
    }
//------------------------------------------------------------------------------

    private class Background extends JPanel{
        public Background(){
            add(b);
            repaint();
        }
//------------------------------------------------------------------------------    
        Bubble b = new Bubble();
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Color c = Color.cyan;
            g.setColor(c);
            g.fillRect(0, 0,getWidth(), getHeight());
        }
//------------------------------------------------------------------------------
        private class Bubble extends JPanel{
            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.green);
                g.drawOval(0, 0, Background.this.getWidth(), Background.this.getHeight());
            }
        }
//------------------------------------------------------------------------------
    }
}  

Output

enter image description here

Problem

The aim is to draw a cyan window with a green circle on it. Later I will add components to the green circle so it will look like there is a window with a cyan background and a green circle with components in it.
The output however is only the cyan background. No circle.

I tried setting XOR mode to cyan but that did not work either. Am I nesting the classes wrong?



Solution 1:[1]

I have run your code and made the changes.Here is it.However your program does not handle window closing. You have to write that.

package test;
import javax.swing.*;
import java.awt.*;

public class TestWindow extends JFrame
{
    //------------------------------------------------------------------------------
    public static void main(String[] args) {
        new TestWindow();
    }
    //------------------------------------------------------------------------------
    public TestWindow(){
        setSize(300,310);
        this.setUndecorated(true);
        add(new Background());
        setVisible(true);
    }
//------------------------------------------------------------------------------

    private class Background extends JPanel{
        public Background(){
            Bubble b = new Bubble();
            add(b);
        }
        //------------------------------------------------------------------------------    

        @Override public Dimension getPreferredSize()
        {
            return new Dimension(300,300);
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Color c = Color.cyan;
            g.setColor(c);
            g.fillRect(0, 0,getWidth(), getHeight());
        }
        //------------------------------------------------------------------------------
        private class Bubble extends JPanel{

            Bubble()
            {
                setOpaque(false);
            }

            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.green);
                g.fillOval(0, 0, getWidth(), getHeight());
            }

            @Override public Dimension getPreferredSize()
            {
                return new Dimension(300,300);
            }
        }
//------------------------------------------------------------------------------
    }
}  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Extreme Coders