1
我一直在嘗試學習更多關於使用JavaFX的知識,並且在這個程序中,我正在嘗試顯示已經玩過的tic tac腳趾的3x3遊戲。我創建了我的ImageViews並設置了我想要使用的圖像,但是一旦我開始將它們插入到行和列中,我注意到我無法兩次使用同一個圖像。我有一個空的空間,一個X和一個O的圖像。一旦我多次使用一個,我得到一個「運行應用程序時發生異常」。可能是一個菜鳥的錯誤,但一個解釋將不勝感激。爲什麼我不能使用兩次相同的圖像?
package Fresh;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Fresh extends Application {
@Override
public void start(Stage primaryStage) {
//Create a pane and set its properties
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
//imv0 = X image
final ImageView imv0 = new ImageView();
final Image image0 = new Image(Fresh.class.getResourceAsStream("images/x.gif"));
imv0.setImage(image0);
//imv1 = O image
final ImageView imv1 = new ImageView();
final Image image1 = new Image(Fresh.class.getResourceAsStream("images/o.gif"));
imv1.setImage(image1);
//imv2 = empty image
final ImageView imv2 = new ImageView();
final Image image2 = new Image(Fresh.class.getResourceAsStream("images/empty.gif"));
imv2.setImage(image2);
//Place nodes in the pane
pane.add((imv0),0,0);
pane.add((imv1), 1, 0);
//Once I try to use imv0 again "I get an exception while running".
pane.add((imv0),0,1);
//Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowGridPane");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
不應該'ImageView imv01 = new ImageView(image1);'也使用'image0'而不是'image1'? – ItachiUchiha 2015-04-06 06:22:20
是的,它應該;謝謝。我修好了它。 – 2015-04-06 13:08:10