2015-10-05 61 views
1

所以我在创造类似植物大战僵尸游戏的过程,但我碰到的一个小问题:之前,我可以套用完成诸如关卡的触摸,我需要阻止将多个JLabel添加到JPanel。尽管我认为我可能走了一条迂回路线,但JLabel的摆放工作正常。如上所述的问题是,另一个JLabel现在可以添加到预先存在的JLabel下面。我如何设置JPanel接受不超过原始组件(最初的JLabel)?任何援助将不胜感激。防止被添加到一个JPanel组件如果另一个组件已经到位

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; 再次感谢提前!

回答

2

如何设置一个JPanel接受不超过原

更可以使用Container.getComponentCount()方法来检查多少部件已被添加到面板上。

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

+0

返回组件的总数,即88个JPanel。有没有办法获得一个JPanel的容器,而不是FRAME.getContentPane()? – Sculler

+0

你在说什么。你为什么使用内容窗格?某处你有一个'panel.add(someComponent)'语句。那么你检查的面板就是它已经添加了一个组件。例如,您的MouseEvent代码使用'getSource()'方法获取面板。我会认为那是你关心的小组,但你比我更了解逻辑。 – camickr

相关问题