【问题标题】:Adding image to JLabel, and displaying Labels in a GridLayout将图像添加到 JLabel,并在 GridLayout 中显示标签
【发布时间】:2013-12-18 05:54:23
【问题描述】:

我当前的代码有趣地与另一个 IDE(JGrasp)一起工作,尽管我目前正在尝试创建一个使用网络的游戏。 Eclipse 允许在单台计算机上联网。出于某种原因,我发布的这个方法将想象添加到 JLabel 数组中,不适用于 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;
}

【问题讨论】:

  • x的String文件路径是什么?
  • 怎么不工作了?你遇到了什么错误?
  • 图像与类位于同一文件中。每个图像被命名为 0.jpg、1.jpg 等。
  • 没有出现错误,没有显示任何内容,但在其他 IDE 中它们是图像。

标签: java eclipse swing jlabel


【解决方案1】:

如果例如boardSpotArray[0]"firstImage",那么您的相对文件路径将为"firstImage.jpg"。在 Eclipse 的这种情况下,并且不使用任何特殊的加载器或资源获取器,IDE 将首先在项目根目录中查找图像。所以你的文件结构应该是这样的

ProjectRoot
         firstImage.jpg    <-- image as direct child of project root
         src
         bin

编辑:

如果您的图片在src 文件夹中

ProjectRoot
        src
           0.jpg       <-- image in src
           1.jpg
           2.jpg

那么你的路径应该是这样的

new ImageIcon("src/" + x + ".jpg")

【讨论】:

  • 谢谢!对不起,新手问题,eclipse新手。
  • 好的,是的,虽然我是 eclipse 的新手,但我也是 overstack 的新手。
【解决方案2】:

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

"healthy-heart.jpg" 可以替换为任何其他图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多