2014-11-25 11 views
0

我想获得一个分隔符,它会根据母版组件的大小更改其大小。 在我的例子中,我有一个JavaFX Popup,在那里我添加一个VBox。为了这个VBox,我添加了一个HBox。而这个HBox有一个Label,一个Speparator和一个Button。 现在我想要让按钮位于右端,标签位于HBox的左端。我想我必须使用这些组件之间的分隔符来获取空间。JavaFX分隔符不依赖于类的宽度

我该如何处理呢?

我做了这样的事情,但它不工作。

// Box for the Headline 
    HBox headLine = new HBox(); 
    headLine.setPadding(new Insets(5, 5, 5, 5)); 

    // Label with the HeadLine Description in 
    final Label heading = new Label(headLineText); 
    heading.getStyleClass().addAll("popup-label-name"); 

    // Close Button 
    close = new Button("X"); 
    close.setVisible(false); 
    closeButtonHandler(); 

    // Creates an invisble Separator1 
    Separator sep = new Separator(Orientation.HORIZONTAL); 
    sep.setVisible(false); 
    sep.widthProperty().add(m_container.widthProperty().get()); 

    close.getStyleClass().addAll("popup-button", "popup-button-color"); 

    // Adds to the Headline the Data 
    headLine.getChildren().addAll(heading, sep, close); 

变量m_container是VBox!我该如何处理它?

感谢您的帮助:)

+0

你只是想空白?或可见的分隔符? – 2014-11-25 17:20:18

回答

0

最简单的方式(如果不使用像AnchorPane不同的容器)是插入一种无形的,但可膨胀“空间”对象:

void testLabelSpace(HBox box) {   
    Text first = new Text("first"); 
    Text second = new Text("second"); 

    Node space = new HBox();  
    HBox.setHgrow(space, Priority.ALWAYS); 

    box.getChildren().addAll(first, space, second); 
} 
0

如果我没有理解正确的问题,你只需要标签和按钮之间的空白区域。只是告诉Label总是水平增长,并设置其允许它增长到任何尺寸最大宽度:

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

public class HBoxExample extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     HBox hbox = new HBox(); 
     Label label = new Label("Label"); 
     Button button = new Button("Button"); 
     HBox.setHgrow(label, Priority.ALWAYS); 
     label.setMaxWidth(Double.MAX_VALUE); 
     hbox.getChildren().addAll(label, button); 

     primaryStage.setScene(new Scene(hbox, 350, 75)); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
}