I am trying to add an image as background for the jframe. However when I run the code below, the rest of the panels wont show up, only the image. I need the image to be on the backwards, and the rest of the jcomponents showing in front. Plus the image wont be resized to the jframes size, but will remain as it is. Is there any way to fix this? To make it easier to read I only demonstrate one of the jpanels with a jbutton init. The image used is this one:
http://wallpoper.com/wallpaper/black-background-metal-hole-444015
Thanks in advance
public class bcquery extends JPanel implements ActionListener {
public bcquery() {
setLayout(new BorderLayout());
JPanel imagepnl = new JPanel(new BorderLayout());
File file = new File(".jpg");
JLabel labelimg;
try {
labelimg = new JLabel(new ImageIcon(ImageIO.read(file)));
imagepnl.add(labelimg, BorderLayout.CENTER);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
add(imagepnl, BorderLayout.CENTER);
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.PAGE_AXIS));
JButton button1 = new JButton("OK");
JPanel btnpanel = new JPanel(new FlowLayout());
btnpanel.add(button1);
btnpanel.setOpaque(false);
mainpanel.setOpaque(false);
mainpanel.add(btnpanel);
imagepnl.add(mainpanel, BorderLayout.NORTH);
add(imagepnl, BorderLayout.CENTER);
private static void createAndShowGUI() throws IOException {
//Create and set up the window.
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
JFrame.setDefaultLookAndFeelDecorated(true);
//Add content to the window.
frame.add(new bcquery());
frame.setResizable(true);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.TRUE);
try {
createAndShowGUI();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}