user1583010
user1583010

Reputation:

Setting a background image on a JFrame using Swing

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);
        }
    }
}

Upvotes: 1

Views: 4680

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

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

Upvotes: 3

Reimeus
Reimeus

Reputation: 159754

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.

Upvotes: 2

Related Questions