2016-07-25 29 views
-1

我使用GridPane作爲佈局Main,但是當我在我的代碼添加Button像...顯示這種類型的錯誤..JavaFX的錯誤:不兼容的類型:ContentDisplay不能轉換到VPOS

Incompatible types: ContentDisplay cannot be converted to VPos 

我的控制器和 Main類

public class Main extends Application 
{ 

    public static void main(String[] args) 
    { 
     Application.launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) 
    { 

     GridPane grid =new GridPane(); 

     grid.setStyle("-fx-background-color: black;"); 
     Button b1=new Button(); 
     grid.setGridLinesVisible(true); 
     grid.setConstraints(new Button("Check"),3,4,1,2,LEFT,CENTER,Priority.SOMETIMES,Priority.SOMETIMES); 
     grid.add(new Button ("like"), 2, 4); 
     Scene scene =new Scene(grid); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 
+0

請分享更多的細節(FXML +控制器) – oszd93

+0

請幫我@ oszd93 – kashem

回答

0

你已添加靜態導入爲ContentDisplay.CENTER。因此,在這一行中使用:

grid.setConstraints(new Button("Check"),3,4,1,2,LEFT,CENTER,Priority.SOMETIMES,Priority.SOMETIMES); 

但是這種方法需要VPos,這是不可轉讓的ContentDisplay,這就是爲什麼這個不能編譯。

您可以通過更改導入的類來簡單地修復此導入,但我不建議使用靜態導入,因爲它會使代碼難以閱讀(您必須檢查導入以瞭解成員來自哪裏) 。

我建議您輸入HPosVPos並改爲使用此代碼。

此外setColumnConstraints是一個static方法,不應使用變量引用。 (這沒有錯,但這是不好的做法。)作爲static方法,這不會將Button添加爲grid的子項。除了設置約束之外,還需要執行此操作。

Button button = new Button("Check"); 
GridPane.setConstraints(button, 
         3, 4, 
         1, 2, 
         HPos.LEFT, VPos.CENTER, 
         Priority.SOMETIMES, Priority.SOMETIMES); 

grid.getChildren().add(button); 
+0

感謝您的幫助@法比安 – kashem

相關問題