How to manipulate the location of the JLabels, having a wallpaper?

0

I'm trying to make an app that has a wallpaper, but I can not change the JButton or JLabel position. I can only do it if I do not have a background.

Follow my code:

public class teste extends JFrame{

public teste(){

JLabel titulo1 = new JLabel("Testes de local1");

JLabel titulo2 = new JLabel("Testes de local2");
//

ImageIcon imagem = new ImageIcon(getClass().getResource("/fotos/1.png"));

JLabel background = new JLabel(imagem);

//
setLayout(new BorderLayout());

background.add(BorderLayout.SOUTH,titulo2);

add(background);

background.setLayout(new FlowLayout());

background.add(titulo1);
//
setVisible(true);

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("Teste");



}
}
    
asked by anonymous 31.05.2017 / 15:05

1 answer

0

Avoid using JLabel to add background to screen because it is a common component and will not allow you to overlay other components on top of it.

The most recommended way to do this is to create a component of type Container as JPanel , or a container top level , and override the method paintComponent ". In addition to filling the background, it will still allow you to add numerous other components and position them as you wish:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TesteBG extends JFrame {

    public TesteBG() {

        JLabel titulo1 = new JLabel("Testes de local1");

        JLabel titulo2 = new JLabel("Testes de local2");
        //
        BackgroundPanel bgPanel = new BackgroundPanel();
        //
        bgPanel.setLayout(new BorderLayout());

        bgPanel.add(titulo1, BorderLayout.NORTH);

        bgPanel.add(titulo2, BorderLayout.SOUTH);

        setContentPane(bgPanel);

        setSize(400, 400);

        setTitle("Teste");

        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    class BackgroundPanel extends JPanel {

        Image imagem;

        public BackgroundPanel() {

            try {

                imagem = ImageIO.read(getClass().getResource("/fotos/1.png"));

            } catch (IOException e) {

                JOptionPane.showMessageDialog(null, "Não foi possivel ler a imagem !");

            }
        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            g.drawImage(imagem, 0, 0, this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new TesteBG();
        });
    }
}

Note that it is also recommended to configure this panel as the rootPane of the screen using the setContentPane() ". From there, everything you add on the screen should be added to the panel and not directly on the screen, so you do not get any parts without background.

There is a more complex but effective way to apply a background image to a screen without getting stuck to the size of the screen or image, if you want to know how, it is well explained in this answer .

    
31.05.2017 / 15:37