使图标等宽

时间:2018-06-07 04:40:38

标签: javafx javafx-2

基本上我想在Javafx中点击操作时将我的按钮一起调整为一个尺寸。

@FXML
private void OnClickResize(ActionEvent event) {
    VBox vBox = new VBox();

    vBox.setPrefWidth(300);

    //Button btn1 = new Button("Short");
    //Button btn2 = new Button("Super Long Button");

    btn_preview.setMaxWidth(vBox.getPrefWidth());
    btn_pdf_preview.setMaxWidth(vBox.getPrefWidth());
    btn_bilingualfile.setMaxWidth(vBox.getPrefWidth());

    btn_close.setMaxWidth(vBox.getPrefWidth());

    vBox.getChildren().addAll(btn_preview,btn_pdf_preview,btn_bilingualfile,btn_close);
}

但这对于vbox和vbox对我没用,所以我想在javafx中使用tabpane这样的解决方案。

2 个答案:

答案 0 :(得分:0)

实施起来可能不是那么容易和优雅,但你可以尝试一下。你需要某种单身类。如果需要线程安全等,可以更改实际的单例实现。

public class ButtonSizeManager {
    private ButtonSizeManager instance;

    private ButtonSizeManager() {}

    public static final ButtonSizeManager getInstance() {
        if (instance == null) instance = new ButtonSizeManager();

        return instance;
    }

    private static final double LARGE_WIDTH = 100;
    private static final double LARGE_HEIGHT = 30;
    /* Similar constants for medium and small sizes */

    private final ReadOnlyDoubleWrapper width = new ReadOnlyDoubleWrapper(); 
    private final ReadOnlyDoubleWrapper height = new ReadOnlyDoubleWrapper();

    public final void largeSize() {
        width.set(LARGE_WIDTH);
        height.set(LARGE_WIDTH);
    }

    /* Similar methods for medium and small */

    public final ReadOnlyDoubleProperty widthProperty() {
        return width.getReadOnlyProperty();
    }
    public final double getWidth() { this.width.get(); }

    public final ReadOnlyDoubleProperty heightProperty() {
        return height.getReadOnlyProperty();
    }
    public final double getHeight() { this.height.get(); }
}

然后你所有的按钮(是的,这将是乏味的)必须绑定到这个。这意味着如果你有从FXML创建的按钮,那么你需要在FXML中给它们fx:id并在控制器类中获取它们的注入引用。

public class Controller {
    @FXML private Button foo;

    public void initialize() {
        foo.setMinWidth(Region.USE_PREF_SIZE);
        foo.setMinHeight(Region.USE_PREF_SIZE);
        foo.setMaxWidth(Double.MAX_VALUE);
        foo.setMaxHeight(Double.MAX_VALUE);

        foo.prefWidthProperty.bind(ButtonSizeManager.getInstance().widthProperty());
        foo.prefHeightProperty.bind(ButtonSizeManager.getInstance().heightProperty());
    }
}

如果您不想对每个按钮执行此操作,则可以将部分代码移至ButtonSizeManager

public final bindButtonSize(Button button) {
    Objects.requireNonNull(button);

    button.setMinWidth(Region.USE_PREF_SIZE);
    button.setMinHeight(Region.USE_PREF_SIZE);
    button.setMaxWidth(Double.MAX_VALUE);
    button.setMaxHeight(Double.MAX_VALUE);

    button.prefWidthProperty.bind(widthProperty());
    button.prefHeightProperty.bind(heightProperty());
}

要更改大小,只需调用单例类中的方法(例如ButtonSizeManager.largeSize()

答案 1 :(得分:0)

您可以根据大小向根名称添加样式类,并使用CSS样式表来修改按钮的大小,例如通过修改-fx-font-size属性:

的style.css

@Override
public void start(Stage primaryStage) throws Exception {
    ComboBox<String> combo = new ComboBox<>();
    combo.getItems().addAll("small", "medium", "large");
    combo.setValue("medium");

    VBox root = new VBox(combo);
    root.getStyleClass().add("medium");

    combo.valueProperty().addListener((o, oldValue, newValue) -> {
        root.getStyleClass().removeAll(combo.getItems());
        root.getStyleClass().add(newValue);
    });

    for (int i = 0; i < 7; i++) {
        root.getChildren().add(new Button("button " + i));
    }

    Scene scene = new Scene(root);
    scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.show();
}
.small .button {
    -fx-font-size: 8;
}

.medium .button {
}

.large .button {
    -fx-font-size: 20;
}

许多Button的属性see JavaFX CSS Reference Guide也有可用的属性。但请注意,如果您通过内联CSS(Node.style)设置属性或从代码设置相应的属性(例如Button.setPrefWidth),则不会应用这些值。

相关问题