I have been learning Java for a few weeks now and I am really stuck when it comes to applying a background image to a JFrame. Every tutorial I've come across doesn't create Frames the way I do ( I extend JFrame ) or if they do, the instructions aren't clear enough for me to understand.
The code below is from a project of my own so help me practice what I've learned so far. Please could you build on the code below and explain to me what to add and where, so I may have an image as the background to my frame?
One thing I would really appreciate is if you could explain how things work and why there are needed and what they are actually doing - I don't like the idea of blindly copying and pasting what you've done without any clue of how it works. The more depth in explanation, the better; even if it sounds patronising.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MiniPad extends JFrame implements ActionListener {
JPanel pan = new JPanel();
ClassLoader ldr = this.getClass().getClassLoader();
ImageIcon closeImg = new ImageIcon(ldr.getResource("\\images\\buttons\\closeBtn.png"));
JTextArea note = new JTextArea("", 6, 21);
JScrollPane notes = new JScrollPane(note);
JButton close = new JButton(closeImg);
public static void main(String[] args) {
MiniPad padgui = new MiniPad();
} //Instance of GUI
public MiniPad() {
super("Notepad");
setSize(265, 191);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(pan);
setVisible(true);
//Specifications
note.setLineWrap(true);
note.setWrapStyleWord(true);
notes.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
close.setBorderPainted(false);
close.setContentAreaFilled(false);
close.setOpaque(false);
//Adding to JPanel 'pan'
pan.add(notes);
pan.add(close);
close.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == close) {
setVisible(false);
}
}
}