Without a full assessment of your requirements, if you just need to display text on top of a background image, you'd better place a label on top of a custom panel that can color your background.
You get the advantage of a layout manager without mess.
I would start by having a readable gutter. Performing custom painting and Graphics2D Trail .
If this sounds intimidating, JLabel is actually a type of Container , meaning it can actually βcontainβ other components.
Example
Background Panel ...
public class PaintPane extends JPanel { private Image background; public PaintPane(Image image) {
Built with ...
public TestLayoutOverlay() throws IOException { // Extends JFrame... setTitle("test"); setLayout(new GridBagLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); PaintPane pane = new PaintPane(ImageIO.read(new File("fire.jpg"))); pane.setLayout(new BorderLayout()); add(pane); JLabel label = new JLabel("I'm on fire"); label.setFont(label.getFont().deriveFont(Font.BOLD, 48)); label.setForeground(Color.WHITE); label.setHorizontalAlignment(JLabel.CENTER); pane.add(label); pack(); setLocationRelativeTo(null); setVisible(true); }
And just to show that I'm not biased;), an example using labels ...
public TestLayoutOverlay() { setTitle("test"); setLayout(new GridBagLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); JLabel background = new JLabel(new ImageIcon("fire.jpg")); background.setLayout(new BorderLayout()); add(background); JLabel label = new JLabel("I'm on fire"); label.setFont(label.getFont().deriveFont(Font.BOLD, 48)); label.setForeground(Color.WHITE); label.setHorizontalAlignment(JLabel.CENTER); background.add(label); pack(); setLocationRelativeTo(null); setVisible(true); }

source share