2017-11-29 2 views
1

JavaFx 응용 프로그램이 있고 특정 레이아웃을 얻으려면 몇 개의 H 및 VBox를 사용했습니다. 하나의 HBox에서는 텍스트 필드와 Button을 가지고 있으며이 HBox에서 가능한 모든 공간을 차지하고 싶습니다.JavaFx는 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

포스트 당신의 공동 de 또는 FXML. – Sedrick

+2

아마'HBox.setHgrow (textfield, Priority.ALWAYS);와'HBox.setHgrow (button, Priority.ALWAYS);'이지만 코드를 보지 않고서는 알기가 어렵습니다. – Sedrick

+0

@SedrickJefferson 문제는 코드가 꽤 크고 까다 롭긴하지만 조금 게시 할 것입니다. – Lukas

답변

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); 
    } 

} 

전 :

Before

후 :

After