Even I had a problem when displaying multiple rectangles in a jframe with the absolute layout layout ie set to null.
JFrame frame = new JFrame("hello"); frame.setLayout(null); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setSize(600, 600); frame.getContentPane().setBackground(Color.red); frame.getContentPane().add(new Square(10,10,100,100));//My rectangle class is similar to the Square mentioned in the question and it extends JComponent.
However, I was getting problems because the rectangle did not display. Unless you explicitly set the borders to Square (JComponent), this will not work. Even if Square has the location passed in the constructor, setbounds only fixes the problem. Below is the problem, and this is the problem for absolute jframe layouts.
Square square = new Square(10,10,100,100); square.setBounds(10,10,100,100); frame.getContentPane().add(square);
source share