简体   繁体   English

如何使用JLabel将图片添加到JPanel

[英]How to add a picture to a JPanel using a JLabel

When I try to add my picture icon to a JLabel on a panel it doesn't show up at all. 当我尝试将我的图片图标添加到面板上的JLabel时,它根本没有显示。 Can someone tell me what Im doing wrong? 有人可以告诉我我做错了什么吗? Here part of my code: 这是我的部分代码:

    public class GameWindow extends JFrame implements ActionListener
{
    ImageIcon myPicture = new ImageIcon("src/GUI/Images/Face copy.png");
    JLabel picLabel = new JLabel(myPicture);

public GameWindow()

{
    super("Game Window");
    this.setSize(1500, 800);
    this.setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    JPanel tryThis = new JPanel();
    tryThis.setLayout(new BorderLayout());
    this.add(tryThis, BorderLayout.CENTER);
    JPanel grid1 = new JPanel();
    tryThis.add(grid1);

    int e = 4;
    int f = 14;
    JPanel[][] grid = new JPanel[e][f];
    grid1.setLayout(new GridLayout(e,f));

    for(int g = 0; g < e; g++) {
       for(int h = 0; h < f; h++) {
          grid[g][h] = new JPanel();
          grid1.add(grid[g][h]);
          grid[g][h].setBorder(BorderFactory.createTitledBorder("Info"));
       }
    }

    grid[3][3].add(picLabel);

Alright, I've tweaked your code slightly, but I managed to get it to display an image on grid 3,3 (Although I could only see the image after slightly resizing the screen) 好的,我对代码进行了一些微调,但是我设法使其在网格3,3上显示图像(尽管只有在稍微调整屏幕大小后才能看到图像)

public class GameWindow extends JFrame implements ActionListener {

    public GameWindow(){
        super("Game Window");

        setSize(1500, 800);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel tryThis = new JPanel();
        tryThis.setLayout(new BorderLayout());

        add(tryThis, BorderLayout.CENTER);

        JPanel grid1 = new JPanel();
        tryThis.add(grid1);

        int e = 4;
        int f = 14;
        JPanel[][] grid = new JPanel[e][f];
        grid1.setLayout(new GridLayout(e,f));

        for(int g = 0; g < e; g++) {
           for(int h = 0; h < f; h++) {
              grid[g][h] = new JPanel();
              grid1.add(grid[g][h]);
              grid[g][h].setBorder(BorderFactory.createTitledBorder("Info"));
           }
        }

        try {
            // Loads the image
            BufferedImage myPicture = ImageIO.read(MainClassNameGoesHere.class.getResource("/example.png"));
            JLabel picLabel = new JLabel(new ImageIcon(myPicture));

            // Displays the image
            grid[3][3].add(picLabel);
        } catch (IOException exception) {
            exception.printStackTrace();
        }

    }

The main thing that I changed was the way that you are loading in the image. 我更改的主要内容是图像中的加载方式。 I hope this helps, and feel free to ask any questions. 希望对您有所帮助,并随时提出任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM