I'm having trouble adding a JPanel that contains the paintComponent component to the JFrame.
If this is the only thing I add to the frame, it works. But as soon as I add the layout manager and add other components to the JFrame, it no longer shows the panel with the image!
To make this clearer ...
This is the code that works, and JPanel has been shown successfully:
The panel that draws the character (in fact, I'm not trying to draw hello, it's just the code here)
public class SignPanel2 extends JPanel { public int hello; public void paintComponent(Graphics comp) { Graphics g = (Graphics) comp; g.setColor(Color.LIGHT_GRAY); g.fillRect(70, 250, 150, 150); g.setColor(Color.BLACK); if (hello > 0) g.drawString("h",135, 270); if (hello > 1 ) g.drawString("he",135, 270); if (hello > 2) g.drawString("hel",135, 270); if (hello > 3) g.drawString("hell",135, 270); if (hello > 4) g.drawString("hello",135, 270); } }
The frame I put the panel on:
public class SignFrame extends JFrame {
Main class
public class TheSignMain { public static void main (String[] args) { SignFrame signframe = new SignFrame(); } }
The above works great and gives me a frame with the right pattern.
But if I add other components to the frame and add the layout manager, it will no longer show me the picture. even if I use repaint ().
I have to enable the layout manager, otherwise it will add a panel with an image, but not other components. This is what the framework class looks like, and that is where I have problems.
the public SignFrame class extends JFrame {
I am completely new to Java, so any help would be greatly appreciated. Sorry for all this code and thanks for your help!