1

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);
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • You may want to use a `LayoutManager` on `pan` as it's default `LayoutManager` is a `FlowLayout` which might not give you the best results. Have a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html & http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html for some more details – MadProgrammer Aug 07 '12 at 21:14
  • @AlexanderPower : Please do have a look at this link for [HOW TO ADD IMAGES TO YOUR PROJECT](http://stackoverflow.com/a/9866659/1057230) and this small [example](http://stackoverflow.com/a/11372350/1057230) by me, where I had tried to explain a bit. If you still need something more, please ask for it anytime :-) And here is one [example](http://stackoverflow.com/a/11376322/1057230), related to your code sequence as to what you doing wrong in your code. – nIcE cOw Aug 08 '12 at 15:08

2 Answers2

3

As to your question as it's titled

You will want to setup a custom Component (such as a JPanel) and override the paintComponent method

Take a look at

Now, you're going to have some issues, the note pane will block most of the background, obscuring the image. This shouldn't discourage from trying though ;)

You'll want to familiarise yourself with how Swing components can be made transparent as well.

Some feedback ;)

I don't think you need to keep a reference to the Classloader, for what you're using it for, it's just adding weight and complexity to your code. It's not a massive issue, but my first question was, "What's he using the class loader for??"

I'd probably adopt some kind of naming convention as well. Generally been a little more verbose will make it easier to understand the code.

close ?? Is that an action? Maybe something like "closeButton", at least I know it's some type of button

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Better to use:

ImageIcon closeImg = new ImageIcon(getClass().getResource("\\images\\buttons\\closeBtn.png"));

Make sure your image is located in the same location relative to where your class file is output.

Also your close button calls setVisible(false) but does not dispose of any resources or exit the application.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 2
    Instead of `setVisible(false)` use `dispose()`, it will release the resources and exit the application (as per your request `setDefaultCloseOperation(EXIT_ON_CLOSE)`) – MadProgrammer Aug 07 '12 at 21:12
  • Thanks for this. As I'm learning I don't know many of these little tricks you can do - I'm basically following what my book tells me, and that was never mentioned. I'll keep a note of it. :-) –  Aug 07 '12 at 21:35