我試圖在方法setFavoritePicture (Picture pRef)
中設置圖片。 這種方法應該設置一個最喜歡的圖片被調用的主要方法,但我不斷收到編譯器錯誤說nonstatic variable pRef cannot be referenced from a static context
。我是比較新的Java的所以任何幫助,您可以提供我將非常讚賞非靜態變量pRef無法從靜態上下文中引用
public class House
{
String owner;
Picture pRef;
Picture [] picArray;
Picture favPic;
public void showArtCollection()
{
ArtWall aWall = new ArtWall(600,600);
aWall.copyPictureIntoWhere(favPic,250,100);
aWall.copyPictureIntoWhere(picArray[0],51,330);
aWall.copyPictureIntoWhere(picArray[1],151,330);
aWall.copyPictureIntoWhere(picArray[2],351,280);
aWall.show();
}
public House (String param)
{
this.owner = param;
this.picArray = new Picture [3];
this.favPic = new Picture (FileChooser.pickAFile());
this.picArray [0] = new Picture (FileChooser.pickAFile());
this.picArray [1] = new Picture (FileChooser.pickAFile());
this.picArray [2] = new Picture (FileChooser.pickAFile());
}
public void setFavoritePicture (Picture pRef)
{
pRef = favPic;
}
public void setOneOtherPicture (int which,Picture pRef)
{
}
public void swapGivenOtherWithFavorite (int which)
{
Picture tempSaver;
tempSaver = pRef;
pRef = picArray [which];
picArray [which] = tempSaver;
}
public void addPicture (Picture pictureAdded)
{
pRef = pictureAdded;
}
public void showPicture()
{
picArray [0].explore();
picArray [1].explore();
picArray [2].explore();
favPic.explore();
}
public static void main (String [] args)
{
House PhDsHouse = new House ("Mad PH.D.");
PhDsHouse.setFavoritePicture (pRef);
PhDsHouse.swapGivenOtherWithFavorite (2);
PhDsHouse.showArtCollection();
}
}
我不能重現你的編譯器錯誤(雖然承認我必須發明所有缺失的類)。上面的代碼中的哪一行是錯誤? – 2013-05-04 18:50:17
@DuncanJones Jones我只是修復了代碼,因爲我意識到這一塊遺漏了,錯誤來了'PhDsHouse.setFavoritePicture(pRef);' – Bradley 2013-05-04 18:53:43