【问题标题】:JavaFx let HBox Items use all width providedJavaFx 让 HBox 项目使用提供的所有宽度
【发布时间】:2018-05-13 18:37:16
【问题描述】:

我有一个 JavaFx 应用程序,为了获得我的特定布局,我使用了一些 H- 和 VBoxes。在一个 HBox 中,我有一个文本字段和一个 Button,我希望它们占据这个 HBox 中所有可能的空间。

vboxRight = new VBox(); //This is the right VBox
vboxRight.setSpacing(10);
   //This HBox represents the first "Line"
   hboxLine = new HBox();
   hboxLine.setSpacing(10);
   hboxLine.setMinHeight(30);
      //Textbox
      txtField = new TextField();
      txtField.setMinHeight(30);

      //Button
      btn = new Button("Search");
      btn.setMinHeight(30);
      btn.setOnAction(this);
   hboxLine.getChildren().addAll(txtField, btn);
vboxRight.getChildren().addAll(hboxLine);

有没有办法做到这一点?也许用css?

【问题讨论】:

  • 发布您的代码或 FXML。
  • 可能是HBox.setHgrow(textfield, Priority.ALWAYS);HBox.setHgrow(button, Priority.ALWAYS);,但不看代码很难知道。
  • @SedrickJefferson 问题是,代码很大而且很棘手,但我会发布一点。
  • 您需要提供的是stackoverflow.com/help/mcve

标签: java css javafx netbeans hbox


【解决方案1】:

使用HBox.setHgrow() 应该可以解决您的问题。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication50 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        VBox vboxRight = new VBox(); //This is the right VBox
        vboxRight.setSpacing(10);
        //This HBox represents the first "Line"
        HBox hboxLine = new HBox();
        hboxLine.setSpacing(10);
        hboxLine.setMinHeight(30);
        //Textbox
        TextField txtField = new TextField();
        txtField.setMinHeight(30);

        //Button
        Button btn = new Button("Search");
        btn.setMinHeight(30);
        //btn.setOnAction(this);
        HBox.setHgrow(txtField, Priority.ALWAYS);//Added this line
        HBox.setHgrow(btn, Priority.ALWAYS);//Added this line
        hboxLine.getChildren().addAll(txtField, btn);
        vboxRight.getChildren().addAll(hboxLine);

        Scene scene = new Scene(vboxRight, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

之前:

之后:

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 2014-03-29
    • 2012-08-25
    • 1970-01-01
    • 2014-03-15
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    相关资源
    最近更新 更多