【问题标题】:Adding a JLabel to JPanel disrupts the items in it将 JLabel 添加到 JPanel 会破坏其中的项目
【发布时间】:2017-05-21 04:52:51
【问题描述】:

我正在尝试创建一个简单的平台游戏,并且我正在尝试将角色放到 JPanel 上。我遇到了一个问题。如果不移动已放置的图块(草、天空等),我无法将角色添加到 JPanel(请注意,角色是包含图像图标的 JLabel 形式)。

我用来放置积木的代码是:

static void drawScreen() throws IOException {
    panel.removeAll();
    int tile = 0;
    int line = 0;
    for (int i = 0; i < t.length; i++, tile++) {
        boolean tD = tile % 32 == 0;
        if (tD) {
            tile = 0;
            line++;
        }
        if (t[i] == 0) {
            File f = new File(sPath);
            BufferedImage s = ImageIO.read(f);
            JLabel l = new JLabel(new ImageIcon(s));
            c.gridx = tile;
            c.gridy = line;
            c.insets = new Insets(0, 0, 0, 0);
            panel.add(l, c);
        }
        if (t[i] == 1) {
            File f = new File(gPath);
            BufferedImage g = ImageIO.read(f);
            JLabel l = new JLabel(new ImageIcon(g));
            c.gridx = tile;
            c.gridy = line;
            c.insets = new Insets(0, 0, 0, 0);
            panel.add(l, c);
        }
        if (t[i] == 2) {
            File f = new File(dPath);
            BufferedImage d = ImageIO.read(f);
            JLabel l = new JLabel(new ImageIcon(d));
            c.gridx = tile;
            c.gridy = line;
            c.insets = new Insets(0, 0, 0, 0);
            panel.add(l, c);
        }
    }

    frame.revalidate();
    frame.repaint();
}

数组 t 包含所有的 tile id。它包含 672 个整数,大部分为 0。

谁能告诉我应该如何在不移动其他图块的情况下将角色添加到特定坐标。

我目前的添加方式是:

static void addChar() throws IOException {

    File f = new File(cPath);
    BufferedImage c1 = ImageIO.read(f);
    BufferedImage c = runResize(c1, 50, 76);

    JLabel l = new JLabel(new ImageIcon(c));
    l.setOpaque(false);
    panel.add(l);

    frame.revalidate();
    frame.repaint();
}

当我运行它时,它会输出:(请原谅我的糟糕艺术)

输出图像:

如果您有任何问题,请告诉我。

【问题讨论】:

    标签: java


    【解决方案1】:

    如果不移动已放置的图块(草、天空等),我无法将角色添加到 JPanel(请注意,角色是包含图像图标的 JLabel 形式)。

    如果您打算在面板上使用真实组件来表示草地、天空,那么您不能再向该面板添加任何组件。

    您可以尝试使用JLayeredPane。这将允许您在彼此之上绘制多个组件。因此,一层将包含您的带有草和天空的面板。下一层将包含您要添加的“字符”。阅读 Swing 教程中有关如何使用 LayeredPanes 的部分以获取更多信息和示例。

    另一个选项是自定义面板的paintComponent() 方法以使用您的图像“绘制”草地和天空。然后您可以将您的“角色”组件添加到面板,因为它现在将是面板上的唯一组件。这可能是更好的方法。

    【讨论】:

      【解决方案2】:

      如果将元素放置在您想要(确切地)放置它们的位置,请考虑使用绝对布局:

      https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

      【讨论】:

        【解决方案3】:

        我认为您需要创建一个具有所需坐标的新实例。 首先,您创建一个新的JLabel 实例:

        JLabel new_l = new JLabel(new ImageIcon(c));
        new_l.setOpaque(false);
        ...
        // Now you need to calculate the coordinates here and check them
        // When your coordinates were covered by `panel`
        // you can put it down on the panel: 
        
        panel.add(new_l);
        

        在另一种情况下,您需要从其他组件中为新角色选择图层。

        【讨论】:

          猜你喜欢
          • 2011-01-03
          • 1970-01-01
          • 1970-01-01
          • 2020-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多