Unable to add paintComponent to frame

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 { // the constructor method public SignFrame () { super("This is the title of the Sign Frame"); setSize(300,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // make a container for the frame Container content = getContentPane(); // call from the drawing panel SignPanel2 signpanel = new SignPanel2(); // change class variable of SignPanel signpanel.hello = 5; signpanel.repaint(); // add signpanel to container content.add(signpanel); setContentPane(content); setVisible(true); } } 

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 {

 // the constructor method public SignFrame () { super("This is the title of the Sign Frame"); setSize(300,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // make a container for the frame Container content = getContentPane(); // need a layout manager to decide the arrangement of the components on the container FlowLayout flo = new FlowLayout(); // designate the layout manager to the container content.setLayout(flo); // make components JPanel buttons = new JPanel(); JButton play = new JButton("Play"); JButton pause = new JButton("Pause"); JButton stop = new JButton("Stop"); // add components to a panel buttons.add(play); buttons.add(pause); buttons.add(stop); // add panel to frame container content.add(buttons); // call from the drawing panel SignPanel2 signpanel = new SignPanel2(); // change class variable of SignPanel signpanel.hello = 5; signpanel.repaint(); // add signpanel to container content.add(signpanel); setContentPane(content); setVisible(true); } } 

I am completely new to Java, so any help would be greatly appreciated. Sorry for all this code and thanks for your help!

+4
source share
1 answer

Not tested, but the layout of the stream probably uses the preferred size of your panel, and you probably did not override getPreferredSize() to return something other than the value [0, 0].

In addition, you must encapsulate the change in the hello variable in the setHello() method, which calls repaint() . The calling code does not have to deal with redrawing. Panl needs to know when it should be repainted, and rename itself.

+4
source

Source: https://habr.com/ru/post/1395884/


All Articles