【问题标题】:JavaFX extend Button and add properties through fxmlJavaFX 扩展 Button 并通过 fxml 添加属性
【发布时间】:2017-03-23 11:43:24
【问题描述】:

我已经像这样扩展了一个 JavaFX 按钮:

public class ExtendedButton extends Button {
    private ObjectProperty<Enum> x;
    public final void setX(Enum value) {
        x.set(value); 
    }
    public final String getX() {
        return x.get()
    }
}

public enum MyEnum{
    A,
    B
}

现在我希望能够在我的 FXML 中使用它。像这样的:

<ExtendedButton fx:id="xButton" x=MyEnum.A />

我如何做到这一点。

【问题讨论】:

  • 你试过了吗?
  • 是的,我有,无法让它为 Enum 工作。
  • 你应该edit你的问题来展示你尝试了什么并解释它在什么方面不起作用。
  • 我编辑了问题。
  • 想必你只是在复制粘贴代码的时候错过了x的初始化...?而且我认为您打算使用ObjectProperty&lt;MyEnum&gt;

标签: java javafx fxml


【解决方案1】:

您可以使用属性元素而不是属性以及fx:constant attribute

<ExtendedButton fx:id="xButton" >
    <x><MyEnum fx:constant="A" /></x>
</ExtendedButton>

这是一个 SSCCE:

ExtendedButton.java

package application;

import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Button;

public class ExtendedButton extends Button {

    public final ObjectProperty<Enum<?>> buttonState = new SimpleObjectProperty<>(ButtonState.NORMAL);

    public final ObjectProperty<Enum<?>> buttonStateProperty() {
        return this.buttonState;
    }


    public final Enum<?> getButtonState() {
        return this.buttonStateProperty().get();
    }


    public final void setButtonState(final Enum<?> buttonState) {
        this.buttonStateProperty().set(buttonState);
    }

    public ExtendedButton() {
        super();
        styleProperty().bind(Bindings.
                when(buttonState.isEqualTo(ButtonState.CRITICAL)).
                then("-fx-base: red;").
                otherwise(""));


    }

}

ButtonState.java:

package application;

public enum ButtonState { NORMAL, CRITICAL }

扩展按钮测试.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.StackPane?>
<?import application.ExtendedButton?>
<?import application.ButtonState?>
<?import javafx.geometry.Insets?>

<StackPane xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets top="20" right="20" bottom="20" left="20"/>
    </padding>
    <ExtendedButton text="Test" >
        <buttonState>
            <ButtonState fx:constant="CRITICAL"/>
        </buttonState>
    </ExtendedButton>
</StackPane>

ExtendedButtonTest.java:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ExtendedButtonTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("ExtendedButtonTest.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

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

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多