【问题标题】:How do I add a background image on a JFrame using a JLabel?如何使用 JLabel 在 JFrame 上添加背景图像?
【发布时间】:2016-08-06 21:52:30
【问题描述】:

我正在尝试使用 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);

    }


}

【问题讨论】:

  • 您正在添加面板 1,然后添加面板 2,面板 2 位于面板 1 上方,因此您的面板 1 不可见,因此图像不可见。

标签: java jframe bluej


【解决方案1】:

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

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

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

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

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

}

【讨论】:

  • 但是如果我想在框架中的特定位置找到按钮呢?
【解决方案2】:

试试这个,

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

【讨论】:

  • 但是如何添加按钮......它们不会出现!
  • 如何使图像保持与框架相同的大小,并随之调整大小?此外,就像@Yahya 所说,我们添加的任何组件都不会显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 2015-02-12
相关资源
最近更新 更多