如果另一个组件已经到位,则阻止将组件添加到jpanel

时间:2015-10-05 17:07:13

标签: 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; 再次感谢!

1 个答案:

答案 0 :(得分:2)

  

如何将JPanel设置为不接受原始

您可以使用Container.getComponentCount()方法检查已添加到面板的组件数量。

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