I'm trying to add a JComponent to another using java, but just one of them (the top-level) is visible. I need to learn how to create nested panels. I need to learn how to paint what the panel has to show.
Here is my code:
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setTitle("window");
f.setBounds(100,50,500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyComponent mc = new MyComponent(10, 10, 200, 200);
MyComponent mc2 = new MyComponent(30, 30, 100, 100);
mc.add(mc2);
f.getContentPane().add(mc);
f.setVisible(true);
}
static class MyComponent extends JComponent {
private int swx;
private int swy;
private int nex;
private int ney;
public MyComponent(int swx, int swy, int nex, int ney){
super();
this.swx = swx;
this.swy = swy;
this.nex = nex;
this.ney = ney;
}
public void paint(Graphics g){
g.drawRect(swx, swy, nex, ney);
}
}