I've been struggling to do something I guess pretty simple :
I want to create a form (JTextField) with a background image. In order for the form not to cover the background Image, I'm using a JLayeredPane. I've been trying different stuff, nothing seems to work : for some reason, I'm either displaying only the background, or only the JTextField, but never both. My goal would be to have a background image that never changes, and just use my buttons / textfields on top of it.
package gestion;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Main extends JFrame{
JLayeredPane layeredPane;
JPanel board;
JPanel background;
public Main(){
super("Test");
background = new JPanel();
layeredPane = new JLayeredPane();
board = new JPanel();
// Creating frame with LayeredPane
Dimension boardSize = new Dimension(1280, 1024);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(boardSize.width, boardSize.height);
this.setVisible(true);
this.setLocationRelativeTo(null);
layeredPane.setPreferredSize( boardSize );
this.add(layeredPane);
// Add a background to the Layered Pane
JLabel picLabel = new JLabel(new ImageIcon("background.jpg"));
background.add(picLabel);
background.setPreferredSize(boardSize);
background.setBounds(0,0,boardSize.width, boardSize.height);
layeredPane.add(background, JLayeredPane.DEFAULT_LAYER);
// Add a JTextField
final JTextField jtf = new JTextField("Default Value");
Font police = new Font("Arial", Font.BOLD, 14);
jtf.setFont(police);
jtf.setPreferredSize(new Dimension(600, 800));
background.setBounds(0,0,boardSize.width, boardSize.height);
jtf.setForeground(Color.BLUE);
board.add(jtf);
layeredPane.add(board, JLayeredPane.PALETTE_LAYER);
}
public static void main(String[] args)
{
new Main();
}
}
Only the image seems to appear, and for some reason (My best bet is dark magic) the JTextField is not there. Any ideas or help would be greatly appreciated ! Thank you !