Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.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 将图像添加到JLabel,并在GridLayout中显示标签_Java_Eclipse_Swing_Jlabel - Fatal编程技术网

Java 将图像添加到JLabel,并在GridLayout中显示标签

Java 将图像添加到JLabel,并在GridLayout中显示标签,java,eclipse,swing,jlabel,Java,Eclipse,Swing,Jlabel,有趣的是,我当前的代码与另一个IDE(JGrasp)一起工作,尽管我目前正在尝试创建一个使用网络的游戏。Eclipse允许在一台计算机上联网。出于某种原因,Im发布的这个向JLabel数组添加imagines的方法不适用于eclipse?我是eclipse新手,不知道为什么会发生这种情况 private JPanel createBoard() { // Instantiate Panel with a GridLayout board = new JPanel(); b

有趣的是,我当前的代码与另一个IDE(JGrasp)一起工作,尽管我目前正在尝试创建一个使用网络的游戏。Eclipse允许在一台计算机上联网。出于某种原因,Im发布的这个向JLabel数组添加imagines的方法不适用于eclipse?我是eclipse新手,不知道为什么会发生这种情况

private JPanel createBoard()
{
    // Instantiate Panel with a GridLayout
    board = new JPanel();
    board.setLayout(new GridLayout(10,10));

    // Fill the Panel with an Array of Labels
    // Checks for exception
    boardSpotArray = new JLabel[100];
    try
    {
        for (int x = 0; x < boardSpotArray.length; x++)
        {
            boardSpotArray[x] = new JLabel();
            boardSpotArray[x].setIcon(new ImageIcon(x + ".jpg"));
            board.add(boardSpotArray[x]);

        }
    }
    catch (IndexOutOfBoundsException exception)
    {
        System.out.println("Array drawer not available, " + exception.getMessage());
    }

    // return panel
    return board;
}
private JPanel createBoard()
{
//使用GridLayout实例化面板
board=新JPanel();
board.setLayout(新网格布局(10,10));
//用一组标签填充面板
//检查是否存在异常
boardSpotArray=新的JLabel[100];
尝试
{
对于(int x=0;x
例如,如果
boardSpotArray[0]
“firstImage”
,则您的相对文件路径将是
“firstImage.jpg”
。在Eclipse的这种情况下,在不使用任何特殊加载程序或资源获取程序的情况下,IDE将首先在项目根中查找映像。所以您的文件结构应该如下所示

ProjectRoot
         firstImage.jpg    <-- image as direct child of project root
         src
         bin
new ImageIcon("src/" + x + ".jpg")
那么你的路径应该是这样的

ProjectRoot
         firstImage.jpg    <-- image as direct child of project root
         src
         bin
new ImageIcon("src/" + x + ".jpg")

在Eclipse中为您完成代码

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class oo {
    public static JPanel createBoard()
    {
        // Instantiate Panel with a GridLayout
        JPanel board = new JPanel();
        board.setLayout(new GridLayout(10,10));

        // Fill the Panel with an Array of Labels
        // Checks for exception
        JLabel[] boardSpotArray = new JLabel[100];
        try
        {
            for (int x = 0; x < boardSpotArray.length; x++)
            {
                boardSpotArray[x] = new JLabel();
                boardSpotArray[x].setIcon(new ImageIcon("healthy-heart.jpg"));
                board.add(boardSpotArray[x]);

            }
        }
        catch (IndexOutOfBoundsException exception)
        {
            System.out.println("Array drawer not available, " + exception.getMessage());
        }

        // return panel
        return board;
    }
    public static void main(String[] args){
        JFrame frame=new JFrame();
        JPanel panel=createBoard();
        frame.getContentPane().add(panel);
        frame.setSize(100, 100);
        frame.pack();
        frame.setVisible(true);
    }
}
导入java.awt.GridLayout;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类oo{
公共静态JPanel createBoard()
{
//使用GridLayout实例化面板
JPanel board=新的JPanel();
board.setLayout(新网格布局(10,10));
//用一组标签填充面板
//检查是否存在异常
JLabel[]boardSpotArray=新的JLabel[100];
尝试
{
对于(int x=0;x

“health heart.jpg”
可以用任何其他图像替换。

x的字符串文件路径是什么?
如何工作?出现什么错误?图像与类位于同一文件中。每个图像都被命名为0.jpg、1.jpg等。没有错误,没有显示任何内容,但在其他IDE中它们是图像。谢谢!对于新手的问题,很抱歉,这是eclipse的新手。好的,是的,作为eclipse的新手,我也是overstack的新手。