Javafx按钮运行不正常,窗格没有调整大小

时间:2017-04-08 21:50:41

标签: java javafx alignment javafx-2 pane

目前,下面的代码生成一个BorderPane,其中间有一个GridPane,底部有一个HBox,用于容纳两个按钮。 GridPane中最左侧的窗格包含文本" Name Here"。现在我只想按钮移动文字"在这里命名"向上和向下但他们不会移动文本。

我认为它与特定的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

1 个答案:

答案 0 :(得分:0)

Try using setLayoutY instead of setY:

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

As a sidenote, the Node parent class also has a relocate method for easily changing both the X and Y coordinates: