2017-11-29 5 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

関連する問題