簡體   English   中英

如果已有其他組件,則阻止將組件添加到jpanel中

[英]Prevent a component from being added to a jpanel if another component is already in place

因此,我正在創建類似於《植物大戰僵屍》的游戲,但是我遇到了一個小問題:在應用諸如關卡之類的畫龍點睛之前,我需要防止將多個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; 再次感謝!

我如何將JPanel設置為僅接受原始內容

您可以使用Container.getComponentCount()方法來檢查已向面板添加了多少個組件。

僅在計數為0時添加組件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM