Timothy O'Connell
Timothy O'Connell

Reputation: 89

Setting JPanel/JFrame background image, my 1st time

I'm back with yet another really stupid question. I read some other stackoverflow posts already, and tried to follow the advice, but to no avail. I cannot get the JPanel background to change to an image! I tried moving the image to my c:\, so it's not that.

Please help and thanks in advance!

Login code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;


public class login implements ActionListener{


    JTextField gusername;
    JTextField gpassword;
    static String username;
    static String password;

    void logini() throws IOException {

        JFrame window = new JFrame("Login");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(300, 250);
        window.setResizable(false);
        window.setVisible(true);

        JPanel mainp = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        window.add(mainp);



        BufferedImage myPicture = ImageIO.read(new File("c:\bgd.png"));
        JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
        mainp.add( picLabel );




        c.gridx = 0;
        c.gridy = 1;
        gusername = new JTextField();
        gusername.setText("Username");
        mainp.add(gusername, c);

        c.gridx = 0;
        c.gridy = 2;
        gpassword = new JTextField();
        gpassword.setText(" password ");
        mainp.add(gpassword, c);

        c.gridx = 0;
        c.gridy = 3;
        JButton login = new JButton("Login");
        mainp.add(login, c);

        login.addActionListener(this);
        login.setActionCommand("ok");
    }


    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("ok")){
            try {
                this.username = (gusername.getText());
                this.password = (gpassword.getText());
                System.out.println("0");

            }
            catch(NumberFormatException ex){
                System.out.println("ERROR: Could not preform function: 7424");

            }
        }
    }


}

Error:

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)
    at login.logini(login.java:34)
    at Main.main(Main.java:10)

Upvotes: 1

Views: 1604

Answers (1)

Erick Robertson
Erick Robertson

Reputation: 33068

Escape the backslash in your path.

A backslash is a special escape character in strings. \b is a special character, not a backslash and a b. To get a backslash, you need two of them \\.

BufferedImage myPicture = ImageIO.read(new File("c:\\bgd.png"));

I would suggest using File.separatorChar instead for platform-independence, but C: is hardly platform independent.

Upvotes: 4

Related Questions