Javafx 使图标宽度相等的步骤

Javafx 使图标宽度相等的步骤,javafx,javafx-2,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_

基本上,我希望在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解决方案。

这可能不太容易实现,但您可以尝试一下。你需要一个单身班。如果需要线程安全等,可以更改实际的单例实现

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());
    }
}
如果您不想对每个按钮执行太多操作,则可以将部分代码移动到
按钮管理器中

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

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

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

样式表
还有许多属性可用于
按钮的属性。但是请注意,如果您通过内联CSS(
Node.style
)设置属性或通过代码设置相应的属性(例如
Button.setPrefWidth
),则不会应用这些值。

您不太清楚要做什么。提供一个看起来像未知
节点
ActionEvent
处理程序的方法的代码说明不了什么。在handler方法中创建一个
VBox
,并向其中添加子项,然后扔掉这个
VBox
,也不会告诉我们您要做什么。请看,首先忽略VBox的情况,因为我只是通过VBox给出示例,我的项目中有一个fxml页面,该页面上的所有按钮都在tabpane下,所以我想通过点击事件得到同样大小的按钮,比如如果小的话,所有的按钮都应该缩小,如果大的话,所有的按钮都应该变大。现在还不清楚。你所有的按钮一开始都是一样大小的吗?如果它们大小相同,您是如何使它们大小相同的?它们的大小是否相同,因为您硬编码的大小相同?“小”按钮应该缩小尺寸,或者将它们全部设置为小的特定尺寸(例如30px 15px)?你有没有每个按钮的参考资料?看,现在我所有的按钮都有不同的大小,当我点击“小”按钮时,我想要什么,它应该将所有页面都设置为特定的大小,并且相对于当前的大小,该大小应该更小,并且不仅在一个页面上,还有3-4个相互集成的fxml页面,基本上,它应该为整个项目设置较小的按钮。我希望现在我对你清楚了。fabian我能得到你的电子邮件id或其他什么吗?因为我对我的项目和它的紧急情况确实有一些疑问?@pragatising将其设置为静态。应该是singleton@PragatiSingh这是打字错误,
prefWidthProperty
应该是方法
getInstance()
是一个方法,它甚至不是一个类。您需要在JavaFX上建立您的基础知识。@pragatising只需在单独的java文件中完成即可。单身应该是所有人都看得见的。@pragatising我已经告诉过你了,不要强迫它进入同一个类。将其移动到单独的文件中。
@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();
}