【问题标题】:Javafx buttons not functioning properly, and pane not resizingJavafx 按钮无法正常工作,并且窗格未调整大小
【发布时间】:2017-09-04 04:07:29
【问题描述】:

目前,下面的代码会生成一个 BorderPane,中间有一个 GridPane,底部有一个 HBox,用于容纳两个按钮。 GridPane 中最左侧的窗格包含文本“Name Here”。现在我只希望按钮上下移动“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

【问题讨论】:

    标签: java javafx alignment javafx-2 pane


    【解决方案1】:

    尝试使用setLayoutY 而不是setY

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

    作为旁注,Node 父类还有一个relocate 方法可以轻松更改 X 和 Y 坐标:

    【讨论】:

    • 如果此答案有帮助,请将其标记为已接受,以供将来遇到它的任何人以及我自己使用。
    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多