Hey guys, I am very new to Java and I am trying to create a simple GUI and make the background a picture, I've tried to google and look on this site for the answer and also tried to implement the solutions and I simply cannot get anything to work.

I should also note that the picture I want to use is in my package "assignment3" location, and the image file name is GUIback.jpg

Here is my code:

package assignment3;


import java.awt.*; // Font, Color
import java.awt.event.*; // ActionEvent, ActionListener
import java.sql.SQLException;
import javax.swing.*; // JFrame, JPanel, JLabel, JButton


//class
public class Interface extends JFrame {
    private static final long serialVersionUID = 1L;


    //global variables
    //Member account = new Member();
    JPanel panel;
    JLabel label;
    JButton addEventButton;
    JButton updateEventButton;
    JButton deleteEventButton;
    JButton addMemberButton;
    JButton updateMemberButton;
    JButton deleteMemberButton;
    JButton exitButton;
    JButton searchButton;
    JTextArea statusTextArea;
    JPanel buttonPanel;
    JScrollPane statusPanel;
    JPanel c;
    JTextField textButton;
    Image Image1;

    public Interface() {
        super("User Interface");

        //settings for panel that holds the buttons
        panel = new JPanel();
        panel.setSize(getSize());
        panel.setLocation(0, 0);
        panel.setLayout(null);
        //panel.setBackground(Color.WHITE);

        //settings for event label
        label = new JLabel("Event");
        label.setSize(200, 50);
        label.setLocation(50, 10);
        label.setFont(new Font("SansSerif", Font.BOLD, 20));
        panel.add(label);

        //setting for member label
        label = new JLabel("Member");
        label.setSize(200,50);
        label.setLocation(260, 10);
        label.setFont(new Font("SansSerif", Font.BOLD, 20));
        panel.add(label);

        //settings for Add Event
        addEventButton = new JButton("Add");
        addEventButton.setSize(120, 60);
        addEventButton.setLocation(20, 70);
        panel.add(addEventButton);

        //settings for Update Event
        updateEventButton = new JButton("Update");
        updateEventButton.setSize(120, 60);
        updateEventButton.setLocation(20, 160);
        panel.add(updateEventButton);

        //settings for Delete Event
        deleteEventButton = new JButton("Delete");
        deleteEventButton.setSize(120, 60);
        deleteEventButton.setLocation(20, 250);
        panel.add(deleteEventButton);

        //settings for Add Member
        addMemberButton = new JButton("Add");
        addMemberButton.setSize(120, 60);
        addMemberButton.setLocation(240, 70);
        panel.add(addMemberButton);

        //settings for Update Member
        updateMemberButton = new JButton("Update");
        updateMemberButton.setSize(120, 60);
        updateMemberButton.setLocation(240, 160);
        panel.add(updateMemberButton);

        //settings for Delete Member
        deleteMemberButton = new JButton("Delete");
        deleteMemberButton.setSize(120, 60);
        deleteMemberButton.setLocation(240, 250);
        panel.add(deleteMemberButton);
        //settings for exit button
        exitButton = new JButton("Exit");
        exitButton.setSize(80, 30);
        exitButton.setLocation(150, 400);
        panel.add(exitButton);

        //settings for search button
        textButton = new JTextField("   Search");
        textButton.setSize(80,30);
        textButton.setLocation(150, 345);
        panel.add(textButton);

        //settings for window
        setSize(380, 450);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        add(panel);
        setVisible(true);

            exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("EXIT");
                System.exit(0);
            }
        });
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I want the image to be the background of my GUI. The image is the same size as the window and Jpanel

Edited 5 Years Ago by HappyCat123: n/a

where is your image placed/saved

please edit (for future readers) your post and pack your code between code tags (on right side)

You can create an Image object from your jpeg file, then overide the paintComponent method for your JPanel and draw your Image into it. It's a standard technique, so you'll have no problems Googling for details.

I will do that, but my image is located in Eclipse in the same package as my "interface" class.

You can create an Image object from your jpeg file, then overide the paintComponent method for your JPanel and draw your Image into it. It's a standard technique, so you'll have no problems Googling for details.

After reading this it makes sense logically, but when I actually try to code it i'm lost... I tried googling it, can someone give me a link or sample code or something? I am literally a noob when it comes to Java.

I appreciate your guys help I'm sure it must be frustrating.

This article has been dead for over six months. Start a new discussion instead.