1

我正在尝试按照这个答案向 JFrame 添加背景图片,但遇到了一个奇怪的错误。调试时,我的 url 返回 null,并且弹出一个窗口,提示“找不到类文件编辑器”源,源附件不包含文件 Launcher.class 的源,您可以通过单击更改附件源来更改源附件以下。这意味着什么?

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

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

    }

}

任何sugesstions将appriciated!

4

3 回答 3

4
于 2012-11-17T09:20:57.123 回答
0

使用 JLabel 以不同的方式进行。这是我的最终代码:

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

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

public class DeluxKenoMainWindow extends JFrame 
{


   public DeluxKenoMainWindow()
   {
    initUI();   
   }

   public final void initUI()
   {
     setLayout(null);
     JLabel background = new JLabel(new ImageIcon ("graphics\\formBackground.png"));

     background.setBounds(0,0,600,600);

     //getContentPane().add(new BackgroundImage());
    int xCoord = 85;
     int yCoord = 84;
     Button[] button = new Button[80];
     for(int i = 0; i<80; i++)
     {
         String buttonName = "button" + i;
        if(i % 10 == 0)
        {
            xCoord = 12;
            yCoord +=44;
        }


        if(i % 40 == 0)
            yCoord += 10;


         button[i] = new Button(buttonName, xCoord, yCoord, i+1);
         xCoord += 42;
         getContentPane().add(button[i]);
     }

     add(background);
     setTitle("Delux Keno");
     setSize(600,600);

     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);
        }
    });
   }
   }
于 2012-11-17T13:42:39.100 回答
0

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

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

    }

}
于 2012-11-17T13:46:39.323 回答