1

この回答に従って背景画像を JFrame に追加しようとしていますが、奇妙なエラーが発生します。URL のデバッグ中に null が返され、「クラス ファイル エディタ」ソースが見つからないというポップアップ ウィンドウが表示されます。ソースの添付ファイルにファイル Launcher.class のソースが含まれていません。Chang Attached Source をクリックして、ソースの添付ファイルを変更できます。下。どういう意味ですか?

ここに私がこれまでに持っているコードがあります:

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

    }

}

どんな提案も感謝します!!

4

3 に答える 3

4
于 2012-11-17T09:20:57.123 に答える
0

私が見つけたもう1つの問題は、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 に答える
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 に答える