编程语言
首页 > 编程语言> > 如何在GroupLayout Java中设置Jframe背景图像

如何在GroupLayout Java中设置Jframe背景图像

作者:互联网

我试图为我的框架设置背景图像,但它不起作用.我试过这个链接:

Setting background images in JFrame

代码:

setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/Images/about.png")))));

我尝试将上面的代码添加到我的Contentpane但它不起作用.

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainMenu frame = new MainMenu();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MainMenu() {
    setIconImage(Toolkit.getDefaultToolkit().getImage(MainMenu.class.getResource("/Images/bug-red.png")));
    setTitle("Automated Bug Fixing");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 712, 458);

            contentPane = new JPanel();

    //contentPane.setBackground(new Color(220, 220, 220));
    contentPane.setForeground(new Color(32, 178, 170));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
            *setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/Images/about.png")))));*

解决方法:

基本概念看起来很好.

您可能遇到问题的唯一可能原因是图像不存在.

看起来你正试图引用一个应该存在于Jar上下文中的图像

代替

ImageIO.read(new File("/Images/about.png"))

尝试

ImageIO.read(getClass().getResource("/Images/about.png"))

代替.

此外,不要吞下异常,确保至少记录所有异常

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BackgroundFrameImage {

    public static void main(String[] args) {
        new BackgroundFrameImage();
    }

    public BackgroundFrameImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try {
                    JLabel label = new JLabel(new ImageIcon(ImageIO.read(...))));

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setContentPane(label);
                    frame.setLayout(new BorderLayout());
                    JLabel text = new JLabel("Hello from the foreground");
                    text.setForeground(Color.WHITE);
                    text.setHorizontalAlignment(JLabel.CENTER);
                    frame.add(text);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException | HeadlessException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

}

标签:java,swing,jframe,grouplayout
来源: https://codeday.me/bug/20190901/1780834.html