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

标签 java swing interface jframe imageicon

我刚开始尝试创建 java 接口(interface),并且希望创建一个接口(interface)作为大学项目的一部分。

目前我仍在开发打开界面,但似乎无法为我的框架设置背景图像。我已经观看了所有我能找到的 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");
            }
        }
    }
}

最佳答案

gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));

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

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

关于java - 在java中的JFrame上设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19602810/

相关文章:

java - 如何在java中动态声明实例的泛型类型

java - JTable中添加Image时显示的字符串路径

Java Swing : manage multiple SpringLayout

java - 用objective-c在java中模拟接口(interface)

java - RandomGenerator - 失去 50% 的平面模拟

java - Java如何在Jenkins中使用环境变量?

java - 单击时 JMenuItem 显示一个空的 Jframe

c++ - C++ 核心指南 C35 "A base class destructor should be either public and virtual, or protected and nonvirtual"对于接口(interface)类是否错误?

java - 如何引用具有多个边界的通用返回类型

java - 无法访问完整数组