JLabel 图像数组 [英] JLabel images array

查看:25
本文介绍了JLabel 图像数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将相同的 jlabel 存储图像两次加载到 gridlayout 面板中,但是不是创建图像的两个实例,图像只显示一次然后移动.

如何将pieces数组中相同的JLabel位置存储到boardLabels数组中的多个JLabel中.

谢谢:)

public static JPanel boardPanel = new JPanel(new GridLayout(4, 0));public static JLabelpieces[] = new JLabel[2];私有静态 JLabel[] boardLabels = new JLabel[4];公共主框架(){pieces[0] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece1.png"));pieces[1] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece2.png"));this.add(boardPanel);displayGUIboard();}public static void displayGUIboard() {//错误-pieces[0] 中的标签未复制到boardLabels [0] 和[1] 中板标签[0] = 件[0];板标签[1] = 件[0];boardPanel.add(boardLabels[0]);boardPanel.add(boardLabels[1]);}公共静态无效主(字符串 [] args){MainFrame frame = new MainFrame();frame.setVisible(true);frame.setSize(600, 600);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}

这行得通

 boardLabels[0] = new JLabel(pieces[1]);boardLabels[1] = new JLabel(pieces[1]);

当使用 ImageIcons 时,但我想避免这种情况,因为要更新电路板,我必须删除然后重新加载 JLabels.我宁愿只更新已经加载的标签.

编辑我之前试过这个,但它抛出一个空指针异常...

 boardLabels[0].setIcon(pieces[1]);boardLabels[1].setIcon(pieces[1]);boardPanel.add(boardLabels[0]);boardPanel.add(boardLabels[1]);

解决方案

不要这样做,因为您不能将同一个组件多次添加到可视化容器中.最好使用多个 JLabel,但让它们使用相同的 ImageIcon.ImageIcons 可以轻松使用多次:

public MainFrame() {pieceIcon[0] = new ImageIcon(System.getProperty("user.dir") +"/images/piece1.png");pieceIcon[1] = new ImageIcon(System.getProperty("user.dir") +"/images/piece2.png");this.add(boardPanel);displayGUIboard();}public void displayGUIboard() {boardPanel.add(new JLabel(pieceIcon[0]);boardPanel.add(new JLabel(pieceIcon[0]);}

顺便说一句:注意没有你的变量应该是静态的.

关于您最近的

<块引用>

这有效

boardLabels[0] = new JLabel(pieces[1]);boardLabels[1] = new JLabel(pieces[1]);

<块引用>

当使用 ImageIcons 时,但我想避免这种情况,因为要更新电路板,我必须删除然后重新加载 JLabels.我宁愿只更新已加载的标签."

解决方案
不,您根本不必更改 JLabels.将您的 JLabel 保持在它们所在的位置,但只需使用 JLabel setIcon(...) 方法交换它们持有的图标.

编辑
另外,不要将变量与对象混淆.即使您创建了一堆 JLabel 变量,如果它们都引用同一个 JLabel 对象,您仍然不能多次向容器添加 JLabel 对象.

编辑您声明:

<块引用>

代码是游戏显示功能的一部分.一个整数数组将代表被解释的板(但不在上面的代码中),正确的 Jlabel 图像将被放置到 gridlayout 面板中以显示板的 gui.我已经让显示代码正常工作,但在我当前的版本中,它从板上删除了 jlabels,然后创建了新的 JLabels(piece...)...但我更喜欢它从整数数组中更新自己而不是删除标签,读取数组,然后重新创建标签.

因此创建一个使用 GridLayout 的 JPanel,并用不变的 JLabel 填充它.然后简单地根据 int 数组保存的值更改 JLabels 保存的图标.您可以创建一种方法来简化和自动化此过程.

编辑关于:

<块引用>

edit 我以前试过这个,但它抛出一个空指针异常.

然后像处理任何 NPE 一样解决这个问题.找出哪一行抛出NPE,检查该行的变量,至少有一个为null,然后修复,以便在尝试使用之前初始化变量.

编辑
例如:

import java.awt.Color;导入 java.awt.Graphics;导入 java.awt.GridLayout;导入 java.awt.image.BufferedImage;导入 javax.swing.*;@SuppressWarnings("串行")公共类 GridExample 扩展 JPanel {公共静态最终 int[][] MAP = {{1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2},{1, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2},{1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2},{1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 2},{1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2},{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},{1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2},{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},{1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2},{1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2},{1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2}};public static final Color[] COLORS = {};私有 JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];公共网格示例(){setLayout(new GridLayout(MAP.length, MAP[0].length));for (int r = 0; r 

显示图形用户界面的网格:

I am trying to load the same jlabel stored image twice into a gridlayout panel, however instead of creating two instances of the image, the image is only displayed once then moved.

How can I store the same JLabel position in the pieces array into more than one JLabel in the boardLabels array.

Thanks :)

public static JPanel boardPanel = new JPanel(new GridLayout(4, 0));
public static JLabel pieces[] = new JLabel[2];
private static JLabel[] boardLabels = new JLabel[4];

public MainFrame() {
    pieces[0] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece1.png"));
    pieces[1] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece2.png"));

    this.add(boardPanel);
    displayGUIboard();
}


public static void displayGUIboard() {

    //ERROR - the label in pieces[0] is not copied into both boardLabels [0] and [1]
    boardLabels[0] = pieces[0];
    boardLabels[1] = pieces[0];

    boardPanel.add(boardLabels[0]);
    boardPanel.add(boardLabels[1]);
}

public static void main(String[] args) {
    MainFrame frame = new MainFrame();
    frame.setVisible(true);
    frame.setSize(600, 600);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

This works

    boardLabels[0] = new JLabel(pieces[1]);
    boardLabels[1] = new JLabel(pieces[1]);

when using ImageIcons, but I want to avoid this since to update the board I will have to remove then reload the JLabels. I would prefer to just update the already loaded labels.

edit I tried this before but it throws a null pointer exception...

    boardLabels[0].setIcon(pieces[1]);
    boardLabels[1].setIcon(pieces[1]);

    boardPanel.add(boardLabels[0]);
    boardPanel.add(boardLabels[1]);

解决方案

Don't do this since you can't add the same component more than once to a visualized container. Better to use multiple JLabels but have them use the same ImageIcon. ImageIcons can be used more than once with ease:

public MainFrame() {
    pieceIcon[0] = new ImageIcon(System.getProperty("user.dir") + 
        "/images/piece1.png");
    pieceIcon[1] = new ImageIcon(System.getProperty("user.dir") + 
        "/images/piece2.png");

    this.add(boardPanel);
    displayGUIboard();
}


public void displayGUIboard() {
    boardPanel.add(new JLabel(pieceIcon[0]);
    boardPanel.add(new JLabel(pieceIcon[0]);
}

As an aside: note that none of your variables should be static.

Edit: regarding your recent edit:

This works

boardLabels[0] = new JLabel(pieces[1]);
boardLabels[1] = new JLabel(pieces[1]);

when using ImageIcons, but I want to avoid this since to update the board I will have to remove then reload the JLabels. I would prefer to just update the already loaded labels."

Solution
No you don't have to change JLabels at all. Keep your JLabels where they are, but simply swap the icons that they hold using the JLabel setIcon(...) method.

Edit
Also, don't confuse variables with objects. Even if you create a bunch of JLabel variables, if they all refer to the same JLabel object, you still can't add a JLabel object more than once to a container.

Edit You state:

The code is a part of the display function for a game. An array of integers will represent the board which is interpreted (but not in the above code) and the correct Jlabel images will be placed into a gridlayout panel to display the gui of the board. I have gotten the display code to work fine, but in my current version it removes the jlabels from the board then creates new JLabels(piece...)... but i would prefer it to update itself from the integer array rather than removing the labels, reading the array, then recreating the labels.

So create a JPanel that uses GridLayout and fill it with unchanging JLabels. Then simply change the icons held by the JLabels based on the values held by the int array. You could create a method that simplifies and automates this process.

Edit regarding:

edit I tried this before but it throws a null pointer exception.

Then solve this as you would any NPE. Find out which line throws the NPE, check the variables on the line, at least one is null, and then fix it so that you initialize the variable before trying to use it.

Edit
for example:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class GridExample extends JPanel {
   public static final int[][] MAP = {
      {1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2},
      {1, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2},
      {1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2},
      {1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 2},
      {1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2},
      {1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
      {1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2},
      {1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
      {1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2},
      {1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2},
      {1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2}
   };

   public static final Color[] COLORS = {};
   private JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];

   public GridExample() {
      setLayout(new GridLayout(MAP.length, MAP[0].length));
      for (int r = 0; r < labelGrid.length; r++) {
         for (int c = 0; c < labelGrid[r].length; c++) {
            labelGrid[r][c] = new JLabel();
            labelGrid[r][c].setIcon(Ground.getGround(MAP[r][c]).getIcon());
            add(labelGrid[r][c]);            
         }
      }
   }

   private static void createAndShowGui() {
      GridExample mainPanel = new GridExample();

      JFrame frame = new JFrame("GridExample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

enum Ground {
   DIRT(0, new Color(205,133, 63)), GRASS(1, new Color(0, 107, 60)), 
   WATER(2, new Color(29, 172, 214));
   private int value;
   private Color color;
   private Icon icon;

   private Ground(int value, Color color) {
      this.value = value;
      this.color = color;

      icon = createIcon(color);
   }

   private Icon createIcon(Color color) {
      int width = 24; // how to use const in enum? 
      BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
      Graphics g = img.getGraphics();
      g.setColor(color);
      g.fillRect(0, 0, width, width);
      g.dispose();
      return new ImageIcon(img);
   }

   public int getValue() {
      return value;
   }

   public Color getColor() {
      return color;
   }

   public Icon getIcon() {
      return icon;
   }

   public static Ground getGround(int value) {
      for (Ground ground : Ground.values()) {
         if (ground.getValue() == value) {
            return ground;
         }
      }
      return null;
   }

}

Which shows a GUI grid:

这篇关于JLabel 图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆