Java 如果另一个组件已经就位,则阻止将该组件添加到jpanel
因此,我正在创建一个类似于植物大战僵尸的游戏,但我遇到了一个小问题:在我可以应用关卡等收尾工作之前,我需要防止一个JPanel中添加多个JLabel。放置JLabel效果良好,但我认为我可能走了一条迂回路线。如上所述的问题是,当前可以在预先存在的JLabel下面添加另一个JLabel。如何将JPanel设置为只接受原始组件(初始JLabel)?如蒙协助,将不胜感激Java 如果另一个组件已经就位,则阻止将该组件添加到jpanel,java,swing,game-development,Java,Swing,Game Development,因此,我正在创建一个类似于植物大战僵尸的游戏,但我遇到了一个小问题:在我可以应用关卡等收尾工作之前,我需要防止一个JPanel中添加多个JLabel。放置JLabel效果良好,但我认为我可能走了一条迂回路线。如上所述的问题是,当前可以在预先存在的JLabel下面添加另一个JLabel。如何将JPanel设置为只接受原始组件(初始JLabel)?如蒙协助,将不胜感激 private final JFrame FRAME; private final JPanel squares[][] = new
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);
}
私有最终JFrame;
私人最终JPanel广场[][]=新JPanel[8][11];
专用最终颜色DGY=颜色。深灰色;
专用最终颜色GRN=Color.GREEN;
私有最终颜色BLK=Color.BLACK;
专用最终颜色LGY=颜色。浅灰色;
专用最终颜色GRY=Color.GRAY;
专用最终颜色BLU=Color.BLUE;
私有最终字体F1=新字体(“Tahoma”,1,36);
私有字符串图标=”;
私有内部层=10;
私有void squareGen(int i、int j、颜色列、布尔边框)
{
方格[i][j]。退根地(col);
如果(边界)
{
正方形[i][j].setboorder(BorderFactory.createLineBorder(BLK,1));
}
加上(方格[i][j]);
}
公共游戏GUI()
{
框架=新的JFrame(“ESKOM VS SA”);
框架设置尺寸(1600900);
FRAME.setLayout(新的GridLayout(8,11));
对于(int x=0;x<8;x++)
{
如果(x>1&&x<7)
{
正方形[x][0]=新的JPanel();
平方根(x,0,GRN,真);
}else如果(x==1 | | x==7)
{
正方形[x][0]=新的JPanel();
平方根(x,0,DGY,真);
}否则
{
正方形[x][0]=新的JPanel();
平方根(x,0,BLU,false);
}
对于(int y=1;y<11;y++)
{
正方形[x][y]=新的JPanel();
如果(x==0)
{
平方根(x,y,BLU,false);
}else如果(y==10 | | x==1 | | x==7)
{
平方根(x,y,DGY,真);
}否则
{
平方根(x,y,LGY,真);
addDToPanel(x,y);
}
}
}
JButton btnPause=新JButton();
btnPause.setText(“| |”);
btnPause.setFont(F1);
btnPause.addActionListener(新的java.awt.event.ActionListener()
{
@凌驾
public void actionPerformed(java.awt.event.ActionEvent evt)
{
btnPauseActionPerformed(evt);
}
});
正方形[7][10]。添加(btnPause);
添加按钮(8);
FRAME.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FRAME.setLocationRelativeTo(空);
}
公共无效重新启动()
{
FRAME.dispose();
GameGUI ggui=新的GameGUI();
ggui.setVisible(true);
}
公共void主菜单()
{
FRAME.dispose();
新建StartUpUI().setVisible(true);
}
私有void btnPauseActionPerformed(java.awt.event.ActionEvent evt)
{
新PauseGUI(this).setVisible(true);
}
私有void BTNADDEDFENSEActionPerformed(java.awt.event.ActionEvent evt)
{
JButton btn=(JButton)evt.getSource();
icon=btn.getActionCommand();
}
专用无效添加按钮(int x)
{
JButton[]btn=新JButton[x];
对于(int j=1;j
我如何设置JPanel,使其只接受原始版本
您可以使用Container.getComponentCount()
方法来检查已向面板添加了多少组件
仅在计数为0时添加组件。这将返回组件的总数,即88个JPanel。是否有方法获得JPanel的容器,而不是FRAME.getContentPane()?您在说什么?为什么要使用内容窗格?在某个地方有一个面板。add(someComponent)
语句。这就是您检查的面板,它添加了一个组件。例如,您的MouseEvent代码使用getSource()
方法获取面板。我想这就是您关心的面板,但您比我更了解逻辑。