我想为从JPanel扩展的Cell设置布局,但它在ObjectListener类中的public void actionPerformed(ActionEvent e)中不起作用 任何人都可以帮忙吗? 这是我的代码:
public class Cell extends JPanel
{
ArrayList<GameObject> objects = new ArrayList<GameObject>();
Cell()
{
setBackground(Color.orange);
setLayout(new GridLayout(2, 2));
GameObject gameObject = new GameObject();
objects.add(gameObject);
add(gameObject);
gameObject.addActionListener(new ObjectListener(this));
}
}
public class ObjectListener implements ActionListener
{
Cell cell;
ObjectListener(Cell cell)
{
this.cell = cell;
}
public void actionPerformed(ActionEvent e)
{
cell.removeAll();
GridLayout gridLayout = new GridLayout(2, 2, 3, 3);
cell.setLayout(gridLayout);
GameObject x = new GameObject();
cell.add(x);
GameObject y = new GameObject();
cell.add(y);
cell.repaint();
/* the gridlayout is not working here */
}
}
public class GameObject extends JButton
{
GameObject()
{
setVisible(true);
setContentAreaFilled(false);
setSize(10, 10);
}
}