JPanel 或 JLabel 似乎为每个对象创建两个标签而不是一个?

发布于 2024-12-09 12:50:33 字数 2795 浏览 1 评论 0原文

如果这是一个非常简单的解决方案或愚蠢的错误,请原谅我 - 这是我第一次尝试用 Java 实现图形! :)

我正在尝试制作一块图块板,每个图块都是一个图块对象,图块的所有位置都存储在名为“内容”的图块三重数组中(内容[][][])。

为了使每个图块“可点击”,我基本上使用每个图块图标创建一个标签,并根据图块对象的 x,y 坐标将该图块放置在板上。我对内容数组中的每个非空 Tile 对象执行此操作。

当我使用 Graphics.drawImage 函数时,效果很好,但是当我使用 setBorders() 函数定位每个标签时,它:

  1. 创建图块的布局,但并不完美 - 似乎有些缺失或低于其他。

  1. 会在位于正确位置的其他图块上方创建一个重复的未定位图层。

我正在调用的函数的代码是:

    public void paintComponent(Graphics graphics) {
    // let superclass paint to fill in background
    super.paintComponent(graphics);
    Tile[][][] content = b.getContent();

    if (content==null || tileImages==null) {
        return;
    }

    /* Set dummy previous label */ 
    prevT.setBounds(-1,-1,1,1);

    // draw tiles back to front

    for (int i = 0; i<content.length; i++) {
        for (int y = 0; y<content[i].length; y++) {
            for (int x = 0; x<content[i][y].length; x++) {
                final Tile t = content[i][y][x];
                if (t!=null) {
                    if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
                        continue;
                    }
                    if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
                        continue;
                    }
                    Image image = tileImages[t.getValue()][t.getSubindex()];

                    if (b.free(t)) {
                        image = tileImagesHL[t.getValue()][t.getSubindex()];

                    }

                    /* Mouse event magic */

                    graphics.drawImage(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);

                    /* Create icon to be displayed */
                    ImageIcon icon = new ImageIcon(image);
                    /* Label that acts as the clickable tile */
                    final JLabel label = new JLabel();

                    /* Associate image with label */
                    label.setIcon(icon);

                    /* Allow mouse events to interact with label */
                    label.addMouseListener(this);

                    /* Position labels according to tile coordinates */
                    label.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));

                    /* Associate label with specified tile */
                    t.setLabel(label);

                    /* Add label to list*/
                    labels.add(label);

                    this.setVisible(true);
                    this.add(label);

                }
            }
        }
    }
}

任何关于为什么会发生这种情况的解释都将非常感激!我已经尝试重写这个函数很多次了,但我没有主意了!

谢谢你! :)

Please excuse me if this is a very simple solution or stupid mistake - this is my first time attempting to implement graphics in Java! :)

I'm attempting to make a board of tiles, each is a Tile object, and all positions of the tiles are stored in a triple array of Tiles called 'content' (content[][][]).

In order to make each tile "clickable", I'm basically creating a label with each tile icon and positioning that tile on the board based on the Tile object's x,y coordinates. I do this for each of the non-null Tile objects in the content array.

This works fine when I use the graphics.drawImage function, but when I position each label using the setBorders() function it:

  1. Creates the layout of tiles, but not perfectly - it seems some are missing or below others.

and

  1. It creates a duplicate unpositioned layer above the other tiles that are in their sort-of correct positions.

The code for the function I'm calling is:

    public void paintComponent(Graphics graphics) {
    // let superclass paint to fill in background
    super.paintComponent(graphics);
    Tile[][][] content = b.getContent();

    if (content==null || tileImages==null) {
        return;
    }

    /* Set dummy previous label */ 
    prevT.setBounds(-1,-1,1,1);

    // draw tiles back to front

    for (int i = 0; i<content.length; i++) {
        for (int y = 0; y<content[i].length; y++) {
            for (int x = 0; x<content[i][y].length; x++) {
                final Tile t = content[i][y][x];
                if (t!=null) {
                    if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
                        continue;
                    }
                    if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
                        continue;
                    }
                    Image image = tileImages[t.getValue()][t.getSubindex()];

                    if (b.free(t)) {
                        image = tileImagesHL[t.getValue()][t.getSubindex()];

                    }

                    /* Mouse event magic */

                    graphics.drawImage(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);

                    /* Create icon to be displayed */
                    ImageIcon icon = new ImageIcon(image);
                    /* Label that acts as the clickable tile */
                    final JLabel label = new JLabel();

                    /* Associate image with label */
                    label.setIcon(icon);

                    /* Allow mouse events to interact with label */
                    label.addMouseListener(this);

                    /* Position labels according to tile coordinates */
                    label.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));

                    /* Associate label with specified tile */
                    t.setLabel(label);

                    /* Add label to list*/
                    labels.add(label);

                    this.setVisible(true);
                    this.add(label);

                }
            }
        }
    }
}

Any explanation for why this is happening would be greatly greatly appreciated! I've tried to re-write this function SO many times and am out of ideas!

Thank you! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-12-16 12:50:33

1)不要在paintComponent(Graphicsgraphics)中创建Object,在

我的视图之前准备这些Object,简单的容器与鼠标事件魔术没有< code>MouseListener

2) 创建 JPanel 并放置 JButton#setBackground(someColor),然后添加JButton#setRolloverIcon(someIcon),不再需要任何 MouseListener

3) 如果您创建 Tiles,则查找 GridLayout

4) 准备 Icons 并将这些 Icon 添加到JButton,没有别的,对象的任何Array,没有比我描述的更多的代码

在此处输入图像描述 在此处输入代码

import java.awt.*;
import javax.swing.*;

public class TilesWithButton {

    private Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon errorIconRoll = UIManager.getIcon("OptionPane.errorIcon");
    private JPanel myPanel = new JPanel();

    public TilesWithButton() {
        myPanel.setLayout(new GridLayout(1, 2, 1, 1));

        JButton btn = new JButton();
        btn.setBackground(Color.white);
        btn.setIcon(infoIcon);
        btn.setRolloverIcon(errorIconRoll);
        btn.setFocusPainted(false);
        myPanel.add(btn);

        JButton btn1 = new JButton();
        btn1.setBackground(Color.white);
        btn1.setIcon(warningIcon);
        btn1.setRolloverIcon(errorIconRoll);
        btn1.setFocusPainted(false);
        myPanel.add(btn1);

        JFrame frame = new JFrame("Tiles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(myPanel);
        frame.pack();
        frame.setLocation(150, 100);
        frame.setVisible(true);
    }

    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TilesWithButton tilesWithButton = new TilesWithButton();
            }
        });
    }
}

1) don't create Object inside paintComponent(Graphics graphics), prepare these Object before

my view, simple comtainer with Mouse event magic without MouseListener

2) create JPanel and put there JButton#setBackground(someColor), and add JButton#setRolloverIcon(someIcon), no longer any MouseListener needed

3) if you create Tiles then look for GridLayout

4) prepare Icons and add these Icon to the JButton, nothing else, any Array of Objects, no code more than I described

enter image description here enter code here

import java.awt.*;
import javax.swing.*;

public class TilesWithButton {

    private Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon errorIconRoll = UIManager.getIcon("OptionPane.errorIcon");
    private JPanel myPanel = new JPanel();

    public TilesWithButton() {
        myPanel.setLayout(new GridLayout(1, 2, 1, 1));

        JButton btn = new JButton();
        btn.setBackground(Color.white);
        btn.setIcon(infoIcon);
        btn.setRolloverIcon(errorIconRoll);
        btn.setFocusPainted(false);
        myPanel.add(btn);

        JButton btn1 = new JButton();
        btn1.setBackground(Color.white);
        btn1.setIcon(warningIcon);
        btn1.setRolloverIcon(errorIconRoll);
        btn1.setFocusPainted(false);
        myPanel.add(btn1);

        JFrame frame = new JFrame("Tiles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(myPanel);
        frame.pack();
        frame.setLocation(150, 100);
        frame.setVisible(true);
    }

    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TilesWithButton tilesWithButton = new TilesWithButton();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文