JButton and JLabel disappears when adding custom background. I don’t see any problems in my program, but maybe you guys find an solution! I think it’s only a little thing I forgot, but I can’t figure it out.
Here’s the code:
GameWindow.java:
setContentPane(new StartImagePanel(RollrackLogo));
out.println("adding JLWelcome");
JLWelcome.setText("Welcome to Rollrack, " + namewindow.name);
add(JLWelcome);
JLWelcome.setVisible(true);
out.println("JLWelcome added");
out.println("adding JBRandom");
JBRandom.setText("Random");
add(JBRandom);
JBRandom.setVisible(true);
out.println("added JBRandom");
The background appears perfect, but not the JButton and JLabel!
Code to the StartImagePanel.java:
public class StartImagePanel extends JComponent{
private Image image;
public StartImagePanel(Image image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}
Your button and label are added to your
GameWindowframe while they should be added to its contentPane,setContentPane(new StartImagePanel(RollrackLogo));instead. That’s why they are not showing, they are added to the frame.Make a variable of the
StartImagePaneland add the button and label to it and they should show up.Answer dispute
The claims in the first paragraph are plain wrong. Here is source that proves it.
Edit after the custom panel code was given
Here’s a snippet that works to show both button and label on a black image background, I removed that was not needed (listeners).