Place the JLabel on top of the JLabel with the image in

I am sure this question has been asked before, but my case is slightly different, since I am trying to place JLabel on top of JLabel acting as a background, I want to display changing numbers using JLabels and the numbers should be displayed in the background, however I am rocking n00b a bit, thanks in advance, jonathan

+4
source share
3 answers

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) { // This is just an example, I'd prefer to use setters/getters // and would also need to provide alignment options ;) background = image; } @Override public Dimension getPreferredSize() { return background == null ? new Dimension(0, 0) : new Dimension(background.getWidth(this), background.getHeight(this)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (background != null) { Insets insets = getInsets(); int width = getWidth() - 1 - (insets.left + insets.right); int height = getHeight() - 1 - (insets.top + insets.bottom); int x = (width - background.getWidth(this)) / 2; int y = (height - background.getHeight(this)) / 2; g.drawImage(background, x, y, this); } } } 

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); } 

On fire

+9
source

At runtime:

  • remove shortcut from its parent
  • add a container that supports layers
  • add a 2x layer but keep the order Z

Enjoy. (The full code is not specified for copy-paste)

0
source

you can do it like this:

 JLabel l1=new JLabel(); JLabel l2=new JLabel(); l1.add(l2, JLabel.NORTH); 
-3
source

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


All Articles