I have the following code as taken from the book I read from:
import java.awt.*;
import javax.swing.*;
/** Frame1 is a frame with a label and a button */
public class Frame1 extends JFrame
{ /** Constructor Frame1 creates a frame with a label and button */
public Frame1()
{ JLabel label = new JLabel("Press This:");
JButton button = new JButton("OK");
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(button);
c.add(label);
setTitle("Example1");
setSize(300, 260);
setVisible(true);
}
public void paint(Graphics g)
{ g.setColor(Color.red);
g.fillRect(0, 0, 100, 100);
}
public static void main(String[] args)
{ new Frame1(); }
}
I expect it to show a red area in the shape of a box on the top left corner of the frame, and the label and button next to it. However, what happens is this: it either shows everything except the red area, or it shows nothing but the frame and it's borders, leaving my jGRASP seeable through the frame area.
I had the same exact problem working with JPanel (this time we're working with JFrame). What solved the problem is puting the getContentPane() in the same position it is now relative to other methods inside the constructor method. However, that's not helping me this time.
Anyone knows what's going on?
P.S. The problem only started after I added to the code the method paint.