在另一个 class (Java Applet) 中迭代链表

Iterating through a Linked List in another class (Java Applet)

当小程序按钮被按下时,我有这个代码:

public void actionPerformed(ActionEvent e)
  {
    oval p;
    int x, y, height, width, fill;
    x = Integer.parseInt(xfield.getText());
    y = Integer.parseInt(yfield.getText());
    height = Integer.parseInt(heightf.getText());
    width = Integer.parseInt(widthf.getText());
    list.add(new oval(x,y,height,width));
    repaint();
}

我需要遍历椭圆列表并在此 class:

中绘制它们
public void draw(Graphics g)
 {
   ListIterator li;
   li = list.listIterator();
   while(li.hasNext())
   {
   g.drawOval(x, y, height, width);
   } 
}

如何将我的列表放入此 class?有什么办法可以将它作为参数传递给 class 吗?

您可以将 private List<oval> ovals; 列表添加到包含 actionPerformed 方法的 class 中,然后将椭圆添加到该列表中(就像您已经在做的一样) .

然后您创建一个 public List<oval> getOvals() 方法,并使用它从另一个 class.

中的 draw 方法访问列表