Java 设置JPanel/JFrame背景图像,我的第一次

Java 设置JPanel/JFrame背景图像,我的第一次,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我又问了一个非常愚蠢的问题。我已经读了一些其他的stackoverflow帖子,并试图按照建议去做,但没有效果。我无法将JPanel背景更改为图像!我尝试将图像移动到我的c:\,所以不是这样 请提前帮助和感谢 登录代码: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import jav

我又问了一个非常愚蠢的问题。我已经读了一些其他的stackoverflow帖子,并试图按照建议去做,但没有效果。我无法将JPanel背景更改为图像!我尝试将图像移动到我的c:\,所以不是这样

请提前帮助和感谢

登录代码:

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

            }
        }
    }


}
错误:

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)

避开路径中的反斜杠。

反斜杠是字符串中的特殊转义字符<代码>\b是一个特殊字符,不是反斜杠和b。要获得反斜杠,需要两个反斜杠
\\

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

我建议使用
File.separatorChar
来代替平台独立性,但是
C:
很难独立于平台。

避开路径中的反斜杠。

反斜杠是字符串中的特殊转义字符<代码>\b是一个特殊字符,不是反斜杠和b。要获得反斜杠,需要两个反斜杠
\\

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

我建议使用
File.separatorChar
来代替平台独立性,但是
C:
很难独立于平台。

另一个建议是从应用程序中读取字符串,并使用它来代替硬编码位置。请确保将程序移动到任何地方,也要将图片移动到任何地方,否则它将无法工作。@Erick Robertson现在图像已打开,为什么我的其他组件没有显示?@TimothyO'Connell这是一个单独的问题,但可能与添加图像时没有为图像设置
GridBagConstraints
有关。因此,它可能位于所有其他组件之上,而你看不到它们。另一个建议是从你的应用程序中读取字符串,并使用该字符串,而不是硬编码你的位置。请确保将程序移动到任何地方,也要将图片移动到任何地方,否则它将无法工作。@Erick Robertson现在图像已打开,为什么我的其他组件没有显示?@TimothyO'Connell这是一个单独的问题,但可能与添加图像时没有为图像设置
GridBagConstraints
有关。因此,它可能位于所有其他组件之上,而您无法看到它们。