如何使用JLabel在JFrame上添加背景图像?

时间:2016-04-15 04:43:10

标签: java jframe bluej

我正在尝试使用JLabel在JFrame上显示背景图像。代码运行并显示按钮,但图像不显示。我已经研究过解决方案,但我还没有找到一个专门的代码。任何帮助将不胜感激。

/**
 * Adds details to interface and programs buttons
 * 
 * Imani Davis 
 * Final Project
 */

import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;


public class Use_PF_Interface extends JFrame implements Pet_Fish_Interface
{
    // instance variables - replace the example below with your own
    private JFrame window;
    private JPanel panel1, panel2, panel3;
    private JLabel lblBackgroundImage = new JLabel();
    private JButton feedButton = new JButton("Feed Fish");
    private JButton playGamesButton = new JButton("Play Game");

    /**
     * Constructor for objects of class Use_PF_Interface
     */
    public Use_PF_Interface()
    {
        setTitle("Virtual Pet Fish");
        setSize(650, 650);


        //initializes panels and panel layout
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel2.setLayout(new FlowLayout());
        panel3.setLayout(new FlowLayout());

        lblBackgroundImage.setLayout(new FlowLayout());

        //sets background image of panel
        lblBackgroundImage.setIcon(new ImageIcon("C:\\Users\\This PC\\Desktop\\OCEAN2.JPEG"));
        panel1.add(lblBackgroundImage);
        validate();

        //adds button to panels
        panel2.add(feedButton);
        panel2.add(playGamesButton);

        //add panels to frame
        add(panel1);
        add(panel2);

    }


}

2 个答案:

答案 0 :(得分:2)

JFrame默认情况下使用BorderLayoutBorderLayout只能管理其提供的五个可用位置中的任何一个中的单个组件,这意味着panel2最多可能是唯一显示的组件。

另一种方法是将您的组件添加到JLabel,但请记住,JLabel没有默认的布局管理器。另外,请记住,JLabel仅使用icontext属性来计算其首选大小,因此如果内容需要更多空间,则会对其进行裁剪。

首先查看How to Use BorderLayout了解更多详情

另外,请记住,大多数Swing组件通常都是不透明的,所以当你想要做这样的事情时你需要将它们设置为透明

Background

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Use_PF_Interface extends JFrame {
    // instance variables - replace the example below with your own

    private JPanel panel2;
    private JLabel lblBackgroundImage = new JLabel();
    private JButton feedButton = new JButton("Feed Fish");
    private JButton playGamesButton = new JButton("Play Game");

    /**
     * Constructor for objects of class Use_PF_Interface
     */
    public Use_PF_Interface() {
        setTitle("Virtual Pet Fish");
        setSize(650, 650);

        //initializes panels and panel layout
        panel2 = new JPanel();
        panel2.setOpaque(false);
        panel2.setLayout(new FlowLayout());

        lblBackgroundImage.setLayout(new FlowLayout());

        //sets background image of panel
        lblBackgroundImage.setIcon(new ImageIcon("..."));
        lblBackgroundImage.setLayout(new BorderLayout());

        //adds button to panels
        panel2.add(feedButton);
        panel2.add(playGamesButton);

        lblBackgroundImage.add(panel2);

        add(lblBackgroundImage);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new Use_PF_Interface();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

答案 1 :(得分:0)

试试这个,

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageInFrame {
    public static void main(String[] args) throws IOException {
        String path = "Image1.jpg";
        File file = new File(path);
        BufferedImage image = ImageIO.read(file);
        JLabel label = new JLabel(new ImageIcon(image));
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(label);
        f.pack();
        f.setLocation(200,200);
        f.setVisible(true);
    }
}