2017-04-08 55 views
0

目前下面的代碼生成一個BorderPane,中間有一個GridPane,底部有一個HBox以容納兩個按鈕。 GridPane中最左邊的窗格包含文本「Name Here」。現在我只想讓按鈕上下移動文本「姓名在這裏」,但他們不會移動文本。Javafx按鈕不能正常工作,窗格不能調整大小

我認爲它與特定的GridPane節點有關,但我不確定。另外,我不知道爲什麼最左邊的GridPane佔用比BorderPane中心最右邊的GridPane更多的空間。

任何意見將不勝感激,謝謝!

import javafx.application.Application; 
    import javafx.stage.Stage; 
    import javafx.geometry.Pos; 
    import javafx.geometry.HPos; 
    import javafx.geometry.VPos; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.layout.Pane; 
    import javafx.scene.layout.BorderPane; 
    import javafx.scene.layout.GridPane; 
    import javafx.scene.layout.StackPane; 
    import javafx.scene.layout.Priority; 
    import javafx.scene.text.Text; 


    public class differentWindows extends Application { 

     protected Text name = new Text("Name Here"); 

     protected BorderPane getPane() { 

     // HBox to hold the up and down buttons 
     HBox paneForButtons = new HBox(20); 
     Button btUp = new Button("Up"); 
     Button btDown = new Button("Down"); 
     paneForButtons.getChildren().addAll(btUp, btDown); 
     paneForButtons.setAlignment(Pos.BOTTOM_LEFT); 

     // Grid pane to go in center of the border pane, for the name and video  
     GridPane paneForTextNVideo = new GridPane(); 
     paneForTextNVideo.setAlignment(Pos.CENTER); 
     paneForTextNVideo.setGridLinesVisible(true); 
     paneForTextNVideo.add(name, 0, 0); 

     Text temp = new Text("temp"); 
     paneForTextNVideo.add(temp, 1, 0); 
     paneForTextNVideo.setHalignment(temp, HPos.CENTER); 
     paneForTextNVideo.setValignment(temp, VPos.CENTER); 
     paneForTextNVideo.setHgrow(temp, Priority.ALWAYS); 
     paneForTextNVideo.setVgrow(temp, Priority.ALWAYS); 

     paneForTextNVideo.setHalignment(name, HPos.CENTER); 
     paneForTextNVideo.setValignment(name, VPos.CENTER); 
     paneForTextNVideo.setHgrow(name, Priority.ALWAYS); 
     paneForTextNVideo.setVgrow(name, Priority.ALWAYS); 

     // Border pane to hold all windows 
     BorderPane pane = new BorderPane(); 
     pane.setBottom(paneForButtons); 
     pane.setCenter(paneForTextNVideo); 

     btUp.setOnAction(e -> name.setY(name.getY() - 10)); 
     btDown.setOnAction(e -> name.setY(name.getY() + 10)); 

     return pane;  

     } // end of the getPane method 


     @Override 
     public void start(Stage primaryStage) { 

     Scene scene = new Scene(getPane(), 450, 200); 
     primaryStage.setTitle("Assignment #7"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     } // end of start method 

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

    } // end of class 

回答

0

嘗試使用的setLayoutY代替setY

btUp.setOnAction(e -> name.setLayoutY(name.getLayoutY() - 10)); 
btDown.setOnAction(e -> name.setLayoutY(name.getLayoutY() + 10)); 

作爲旁註,所述Node父類還具有用於容易地改變兩個X和Y座標一個relocate方法:

+0

如果這個答案有幫助,請將其標記爲對將來和爲我自己而言的任何人都接受。 – corpico