在NetBeans视觉库教程上添加背景图像 [英] Add background image on NetBeans Visual Library Tutorial

查看:242
本文介绍了在NetBeans视觉库教程上添加背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个教程,并会添加背景图片.我尝试这样做:

I'm doing a tutorial and would add a background image. I've tried to do this:

public void run() {
    JFrame frame = new JFrame("VisLibDemo");
    try{
         frame.setContentPane(new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
    }catch(IOException e){
         System.out.println("Error");
    }
    frame.setMinimumSize(new Dimension(500, 400));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new VisLibDemo());
    frame.pack();
    frame.setVisible(true);

}

但是我没有得到.

或者不显示背景图像,或者当它出现在顶部时,其余的将不显示.

Or not showing the background image, or when it appears is on top and the rest will not appear.

这就是我所拥有的:

我想将图像添加到背景...

And I wanted to add image to background...

我这样做:

  public void run() {
        ImageIcon icon = new ImageIcon(
        getClass().getResource("/Images/Brg800.jpg"));
        JLabel label = new JLabel(icon);
        label.setLayout(new BorderLayout());

        JFrame frame = new JFrame("VisLibDemo");
        //frame.add(new VisLibDemo());
        frame.setContentPane(label);
        VisLibDemo demo = new VisLibDemo(); 
        demo.setOpaque(false); frame.add(demo);
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

但是我只能看到一点背景图像("/Images/Brg800.jpg")

But I can only see a little bit of the background image ("/Images/Brg800.jpg")

对不起,不好的解释.但是我需要一些帮助!

Sorry for the bad explanation. But I need some help!

请帮帮我!!

推荐答案

将布局设置为JLabel

JLabel label = new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
label.setLayout(new BorderLayout());
frame.setContentPane(label);


附带说明

在处理将要嵌入到程序中的图像时,您将希望通过资源而不是通过File进行加载,而File在正在开发的系统上无法在其他系统上运行.要按资源使用进行加载

When dealing with images that will be embedded into your program, you will want to load by resource and not by File which will not work on other systems beside the one you are developing on. To load by resource use

getClass().getResource("/path/to/image.png");

您的图像应该在您的src中的某个位置,该图像将被构建到类路径中.所以有了这个文件结构

Your image should be in your src somewhere, where it will be built into the class path. So with this file structure

ProjectRoot
         src
            Pictures
                  Brg800.jpg

您将使用此

ImageIcon icon = new ImageIcon(getClass().getResource("/Pictures/Brg800.jpg"));
JLabel label = new JLabel(icon);
label.setLayout(new BorderLayout());
frame.setContentPane(label);

嵌入式资源 标签Wiki底部的链接中查看更多信息

See more at the link at the bottom of embedded resources tag wiki

以下是上述所有示例

import java.awt.*;
import javax.swing.*;

public class ASimpleExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                ImageIcon icon = new ImageIcon(
                        getClass().getResource("/images/stackoverflow5.png"));
                JLabel label = new JLabel(icon);
                label.setLayout(new BorderLayout());

                JPanel panel = new JPanel(new GridBagLayout());
                panel.setOpaque(false);
                panel.add(new JButton("Button"));

                JFrame frame = new JFrame();
                frame.setContentPane(label);
                frame.add(panel);
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

这篇关于在NetBeans视觉库教程上添加背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆