我有一个JavaFx应用程序,为了获得我的特定布局,我使用了一些H-和VBox。在一个HBox中,我有一个文本字段和一个按钮,我希望它们占用这个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?
答案 0 :(得分: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);
}
}
在:
后: