Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将背景图片添加到JFrame_Java_Swing_Jframe - Fatal编程技术网

Java 将背景图片添加到JFrame

Java 将背景图片添加到JFrame,java,swing,jframe,Java,Swing,Jframe,我试图按照答案在JFrame中添加背景图片,但我遇到了一个奇怪的错误。调试时,我的url返回空值,我会弹出一个窗口,显示“未找到类文件编辑器源”源附件不包含文件启动器的源。您可以通过单击下面的“更改附加源”来更改源附件。这是什么意思 以下是我目前掌握的代码: import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JButton

我试图按照答案在JFrame中添加背景图片,但我遇到了一个奇怪的错误。调试时,我的url返回空值,我会弹出一个窗口,显示“未找到类文件编辑器源”源附件不包含文件启动器的源。您可以通过单击下面的“更改附加源”来更改源附件。这是什么意思

以下是我目前掌握的代码:

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

    }

}
任何建议都将被采纳

不要在paintComponent中执行任何FileIO,将此图像作为局部变量加载一次,在paintComponent中传递变量

宾果游戏,我的文件使用

不要在paintComponent中执行任何FileIO,将此图像作为局部变量加载一次,在paintComponent中传递变量

宾果游戏,我的文件使用


用另一种方法,使用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);
        }
    });
   }
   }

用另一种方法,使用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);
        }
    });
   }
   }

我发现的另一个问题是,您需要为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);

    }

}

我发现的另一个问题是,您需要为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);

    }

}

要绘制背景图像,请选中。问题的其余部分与IDE有关,但我不知道您使用的是哪个IDE。您的类路径中是否有formBackground.png?或者您是否位于运行时可以使用DeluxKenoMainWindow.class的同一目录中。图片文件位于Delux Keno->graphics->picture file下我正在使用eclipse IDE绘制背景图像,请选中。问题的其余部分与IDE有关,但我不知道您使用的是哪个IDE。您的类路径中是否有formBackground.png?或者您是否在运行时可以使用DeluxKenoMainWindow.class的同一目录中。图片文件位于Delux Keno->graphics->picture file下我正在使用eclipse的思想从我可以看出我没有在paintComponent中执行任何文件IO,缓冲的图像被预先读取并存储为变量img。在我的Button类中使用getClass.getResource之后,这似乎是我的问题所在,如果我在那里使用相同的方法,我会得到一堆没有任何图形的按钮。另外,在逐步浏览代码之后,我发现paintComponentGraphics g方法从未被调用,这是为什么?对于扫雷者,我更喜欢使用一个JPanel,其中包含一个JButton和JLabel,所有这些都由一个CardLayout控制。从我可以看出,我没有在paintComponent中执行任何文件IO,缓冲后的图像被预先读取并存储为变量img。在我的Button类中使用getClass.getResource之后,这似乎是我的问题所在,如果我在那里使用相同的方法,我会得到一堆没有任何图形的按钮。同样,在逐步浏览代码之后,我发现paintComponentGraphics g方法从未被调用,为什么会这样?对于扫雷者,我更喜欢使用一个JPanel,它包含一个JButton和JLabel,所有这些都由一个CardLayout控制。