将背景图片添加到JFrame [英] adding background pic to JFrame

查看:79
本文介绍了将背景图片添加到JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照答案添加背景图片到一个JFrame,我得到一个奇怪的错误.调试时,我的url返回null,然后弹出一个窗口,提示未找到类文件编辑器"源.源附件不包含文件Launcher.class的源.您可以通过单击Chang Attached Source来更改源附件.以下.是什么意思?

I'm trying to follow this answer to add a background picture to a JFrame and I'm getting a weird error. While debugging my url is coming back null and I get a window that pops up saying "Class File Editor" source not found the source attachment does not contain the source for the file Launcher.class you can change the source attachment by clicking Chang Attached Source below. What does that mean?

这是我到目前为止的代码:

here's the code that I have so far:

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class DeluxKenoMainWindow extends JFrame 
{


   public DeluxKenoMainWindow()
   {
    initUI();   
   }

   public final void initUI()
   {
     setLayout(null);
     getContentPane().add(new BackgroundImage());
     int xCoord = 10;
     int yCoord = 10;
     Button[] button = new Button[80];
     for(int i = 0; i<80; i++)
     {
         String buttonName = "button" + i;
        if(i % 10 == 0)
        {
            xCoord = 10;
            yCoord +=40;
        }

        xCoord += 40;
        if(i % 40 == 0)
            yCoord += 8;


         button[i] = new Button(buttonName, xCoord, yCoord, i+1);

         getContentPane().add(button[i]);
     }


     setTitle("Delux Keno");
     setSize(500,500);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLocationRelativeTo(null);

   }



   public static void main(String[] args)
   {
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            System.setProperty("DEBUG_UI", "true");
            DeluxKenoMainWindow ex = new DeluxKenoMainWindow();
            ex.setVisible(true);
        }
    });
   }
   }


import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.io.*;

public class Button extends JButton {


    private String name;
    private int xCoord;
    private int yCoord;
    private final int xSize = 40;
    private final int ySize = 40;
    private int buttonNumber;
    private String picture;


    public Button(String inName, int inXCoord, int inYCoord, int inButtonNumber)
    {


      xCoord = inXCoord;
      yCoord = inYCoord;
      buttonNumber = inButtonNumber;
      picture = "graphics\\" + buttonNumber + "normal.png";



      super.setName(name);    
      super.setIcon(new ImageIcon(picture));
      super.setBounds(xCoord, yCoord, xSize, ySize);

    }



    }


import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;


import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class BackgroundImage extends JPanel{

    private BufferedImage img;
    private URL rUrl;
    public BackgroundImage()
    {
        super();

        try{
            rUrl = getClass().getResource("formBackground.png");
            img = ImageIO.read(rUrl);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        //super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

    }

}

任何建议都将适用!

推荐答案

我发现的另一个问题是,您需要对JPanel使用setBounds()使其具有任意大小.为此,我尝试的第一种方法是更新的BackgroundImage类:

another problem I found is that you need to use setBounds() for the JPanel for it to have any size. To do it the first way I tried this is the updated BackgroundImage class:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class BackgroundImage extends JPanel{

    private BufferedImage img;
    private URL rUrl;
    public BackgroundImage()
    {
        super();

        try{
            rUrl = getClass().getResource("formBackground.png");

            img = ImageIO.read(rUrl);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        super.setBounds(0,0,600,600);

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

    }

}

这篇关于将背景图片添加到JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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