在java中的JFrame上设置背景图像

在java中的JFrame上设置背景图像,java,swing,interface,jframe,imageicon,Java,Swing,Interface,Jframe,Imageicon,我对尝试创建java接口还不熟悉,我想在大学项目中创建一个 目前,我仍然只是在打开界面,但似乎不能设置一个背景图像到我的框架。我看了所有我能找到的youtube视频,浏览了所有论坛,但仍然没有任何效果 我看到的所有示例都没有按钮和文本框,所以我不确定这是否是问题所在,但在我的“尝试和捕获”中,我不断地得到“图像不存在”,即使我已将具有正确文件名的图像放入其中 就像我说的,我对界面还不熟悉,所以我知道它可能非常简单,也可能我没有把它弄得一团糟,但是如果有人能帮上忙,我会非常感激的 import j

我对尝试创建java接口还不熟悉,我想在大学项目中创建一个

目前,我仍然只是在打开界面,但似乎不能设置一个背景图像到我的框架。我看了所有我能找到的youtube视频,浏览了所有论坛,但仍然没有任何效果

我看到的所有示例都没有按钮和文本框,所以我不确定这是否是问题所在,但在我的“尝试和捕获”中,我不断地得到“图像不存在”,即使我已将具有正确文件名的图像放入其中

就像我说的,我对界面还不熟悉,所以我知道它可能非常简单,也可能我没有把它弄得一团糟,但是如果有人能帮上忙,我会非常感激的

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.*;

public class CoopWelcome extends JFrame {

    private ImageIcon image1;
    private JButton b1;
    private JTextField userName;
    private static String password = "";
    private JPanel backGround;

    CoopWelcome() {
        setLayout(new FlowLayout());
        //creating username textbox
        userName = new JTextField("");
        userName.setText("Username");
        userName.setForeground(Color.GRAY);
        userName.setColumns(10);
        getContentPane().add(userName);
        //creating password textbox
        JPasswordField passWord = new JPasswordField(10);
        passWord.setEchoChar('*');
        passWord.addActionListener(new AL());
        getContentPane().add(passWord);
        //adding the button and the label to the panel
        b1 = new JButton("something");
        getContentPane().add(b1);
        //getting the image and displaying to the label
    }

    public static void main(String[] Args) {
        //Creating the interface
        CoopWelcome gui = new CoopWelcome();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.pack();
        gui.setTitle("The Co-operative");
        try {
            gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));

        } catch (IOException e) {
            System.out.println("image doesn't exist");
        }
    }

    static class AL implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] passy = input.getPassword();
            String p = new String(passy);
            if (p.equals(password)) {
                JOptionPane.showMessageDialog(null, "Correct");

            } else {
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
        }
    }
}
setContentPane(新的JLabel(新的ImageIcon(ImageIO.read)(新文件(“img.jpg“)))))

这是一种合理的方法,但问题是默认情况下JLabel不使用布局管理器,您无法轻松地向其添加组件。尝试:

JLabel background = new JLabel(...);
background.setLayout( new BorderLayout() );
gui.setContentPane( background );

问题现在解决了

我基本上完全重新编辑了我的JFrame和标签,并设法让它工作。 也许格式仍然很糟糕,但嘿,至少它现在可以工作了,而且现在让未来更容易知道我可能出了什么问题

    CoopWelcome() {

setLayout (new FlowLayout());

//username field

userName = new JTextField("");
userName.setText("Username");
userName.setForeground(Color.GRAY);
userName.setColumns(10);
getContentPane().add(userName);

    //password field

JPasswordField passWord = new JPasswordField(10);
passWord.setEchoChar('*');
passWord.addActionListener(new AL());
getContentPane().add(passWord);

//button

b1 = new JButton("something");
getContentPane().add(b1);


//frame

setSize(500,600);
    setVisible(true); setLayout(new BorderLayout());
    JLabel background=new JLabel(new ImageIcon("img.jpg"));
    add(background);
background.setLayout(new FlowLayout());

}

public static void main(String[] Args){

    new CoopWelcome();

}

这一定是我在这两周的代码中看到的最糟糕的代码格式。但说真的,您的文件路径不正确。您希望使用一个相对于用户目录的路径,该路径可以通过创建文件对象然后打印出其路径或通过以下行找到:
System.out.println(System.getProperty(“user.dir”)。此外,您可能不希望将图像作为文件,而是作为资源。有一个谷歌在上面作为我的意思的例子。有关检索资源的更多信息,请参阅:“…大学项目..设置背景图像..”对于这个项目,将额外的精力投入到设计和文档中,而不是浮华。不要忘了让背景JLabel不透明,因为contentPane应该始终不透明,默认情况下,jlabel不是不透明的。1+。我想我理解你的意思,但现在我有一个问题,就是界面是灰色的,没有文本框或按钮。我很抱歉,如果我在这方面看起来真的很愚蠢,我仍然只是在处理接口,但这是我输入的代码,以防我把它弄糟了。背景=新的JLabel(新的图像图标(“img.jpg”);setLayout(新的BorderLayout());setContentPane(后台);背景设置不透明(真);
    CoopWelcome() {

setLayout (new FlowLayout());

//username field

userName = new JTextField("");
userName.setText("Username");
userName.setForeground(Color.GRAY);
userName.setColumns(10);
getContentPane().add(userName);

    //password field

JPasswordField passWord = new JPasswordField(10);
passWord.setEchoChar('*');
passWord.addActionListener(new AL());
getContentPane().add(passWord);

//button

b1 = new JButton("something");
getContentPane().add(b1);


//frame

setSize(500,600);
    setVisible(true); setLayout(new BorderLayout());
    JLabel background=new JLabel(new ImageIcon("img.jpg"));
    add(background);
background.setLayout(new FlowLayout());

}

public static void main(String[] Args){

    new CoopWelcome();

}