java - 如果另一个组件已经就位,则防止将组件添加到 jpanel

标签 java swing game-development

所以我正在创建一款类似于《植物大战僵尸》的游戏,但是我遇到了一个小问题:在应用关卡等收尾工作之前,我需要防止添加多个 JLabel到 JPanel。 JLabel 的放置效果很好,尽管我认为我可能走了一条迂回路线。如上所述的问题是,当前可以在预先存在的 JLabel 下方添加另一个 JLabel。如何将 JPanel 设置为仅接受原始组件(初始 JLabel)?任何帮助将不胜感激。

private final JFrame FRAME;
private final JPanel squares[][] = new JPanel[8][11];
private final Color DGY = Color.DARK_GRAY;
private final Color GRN = Color.GREEN;
private final Color BLK = Color.BLACK;
private final Color LGY = Color.LIGHT_GRAY;
private final Color GRY = Color.GRAY;
private final Color BLU = Color.BLUE;
private final Font F1 = new Font("Tahoma", 1, 36);
private String icon = "";
private int lvl = 10;

private void squareGen(int i, int j, Color col, boolean border)
{
    squares[i][j].setBackground(col);
    if (border)
    {
        squares[i][j].setBorder(BorderFactory.createLineBorder(BLK, 1));
    }
    FRAME.add(squares[i][j]);
}

public GameGUI()
{
    FRAME = new JFrame("ESKOM VS SA");
    FRAME.setSize(1600, 900);
    FRAME.setLayout(new GridLayout(8, 11));
    for (int x = 0; x < 8; x++)
    {
        if (x > 1 && x < 7)
        {
            squares[x][0] = new JPanel();
            squareGen(x, 0, GRN, true);
        } else if (x == 1 || x == 7)
        {
            squares[x][0] = new JPanel();
            squareGen(x, 0, DGY, true);
        } else
        {
            squares[x][0] = new JPanel();
            squareGen(x, 0, BLU, false);
        }
        for (int y = 1; y < 11; y++)
        {
            squares[x][y] = new JPanel();
            if (x == 0)
            {
                squareGen(x, y, BLU, false);
            } else if (y == 10 || x == 1 || x == 7)
            {
                squareGen(x, y, DGY, true);
            } else
            {
                squareGen(x, y, LGY, true);
                addDToPanel(x, y);
            }
        }
    }
    JButton btnPause = new JButton();
    btnPause.setText("||");
    btnPause.setFont(F1);
    btnPause.addActionListener(new java.awt.event.ActionListener()
    {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            btnPauseActionPerformed(evt);
        }
    });
    squares[7][10].add(btnPause);
    addButtons(8);
    FRAME.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    FRAME.setLocationRelativeTo(null);
}

public void restart()
{
    FRAME.dispose();
    GameGUI ggui = new GameGUI();
    ggui.setVisible(true);
}

public void mainMenu()
{
    FRAME.dispose();
    new StartUpUI().setVisible(true);
}

private void btnPauseActionPerformed(java.awt.event.ActionEvent evt)
{
    new PauseGUI(this).setVisible(true);
}

private void btnAddDefenseActionPerformed(java.awt.event.ActionEvent evt)
{
    JButton btn = (JButton) evt.getSource();
    icon = btn.getActionCommand();
}

private void addButtons(int x)
{
    JButton[] btn = new JButton[x];
    for (int j = 1; j <= x; j++)
    {
        btn[j - 1] = new JButton();
        ImageIcon ii = new ImageIcon(this.getClass().getResource("" + j + ".png"));
        btn[j - 1].setIcon(ii);
        btn[j - 1].setActionCommand("" + j);
        btn[j - 1].setText("");
        btn[j - 1].setForeground(btn[j - 1].getBackground());
        squares[0][j].add(btn[j - 1]);
        btn[j - 1].addActionListener(new java.awt.event.ActionListener()
        {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnAddDefenseActionPerformed(evt);
            }
        });
    }
}

private void addDToPanel(int x, int y)
{
    squares[x][y].addMouseListener(new MouseAdapter()
    {
        @Override
        public void mousePressed(MouseEvent me)
        {
            JPanel panel = (JPanel) me.getSource();

            int j = (int) (panel.getX() / 145.45454545) + 1;
            int i = (int) (panel.getY() / 112.5) + 1;
            addDefense(i, j, icon);
            icon = "";
            FRAME.revalidate();
            FRAME.repaint();
        }
    });
}

private void addDefense(int i, int j, String imageName)
{
    try
    {
        JLabel jlbl = new JLabel();
        ImageIcon ii = new ImageIcon(this.getClass().getResource(imageName + ".png"));
        jlbl.setIcon(ii);
        squares[i][j].add(jlbl, java.awt.BorderLayout.CENTER);

    } catch (Exception e)
    {
    }
}

public void setLvl(int lvl)
{
    this.lvl = lvl;
}

public void setVisible(boolean b)
{
    FRAME.setVisible(b);
}

这不是我的主类,这个类是在主类中实例化的并且setVisible() = true; 再次提前致谢!

最佳答案

How do I set a JPanel to accept no more than the original

您可以使用Container.getComponentCount()方法来检查面板中添加了多少组件。

仅在计数为 0 时添加组件。

关于java - 如果另一个组件已经就位,则防止将组件添加到 jpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954047/

相关文章:

java - Datasnapshot HashMap 不能转换为另一个类

java - Kxml2 如何将xml转换为wbxml?

java - AbstractTableModel 名称 列 sql 中的帮助

c++ - 2D “game”开发:游戏逻辑和绘图的坐标

java - 单次按键即可流畅移动

unity3d - 统一 : How to destroy a gameobject through network?

java - Controller 不支持 Spring

java - 将密码查询结果 (JSON) 解析为 Java 对象

java - 如何使用mysql在jtable中显示Url Image?

java - 单击按钮时如何关闭框架