I need to set a background image for a JFrame
and to display a JLabel
using a BorderLayout
. After looking at other questions I wrote down the following code:
JFrame frame = new JFrame("My window");
JTextField username = new JTextField();
Button startGame = new Button("Enter");
JPanel bottomPanel = new JPanel();
bottomPanel.add(username);
bottomPanel.add(startGame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(bottomPanel, BorderLayout.PAGE_END);
frame.setContentPane(new JLabel(background));
frame.pack();
frame.setVisible(true);
The problem is that bottomPanel, a JPanel
containing a JButton
and a JTextField
, isn't shown on the GUI.
I found out that using a GridBagLayout
as shown in this answer (How to set a background picture in JPanel) actually works but it's not easy to set and I'd prefer to use the default BorderLayout
.