2017-04-08 58 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