将HTML / CSS集成到JavaFX的FXML和CSS中

时间:2015-10-26 02:01:29

标签: java html css javafx fxml

  • 简短版本:

CodePen上的我的HTML / CSS生成一个简单的按钮,可以在悬停时旋转和增长。我想将它集成到我的JavaFX GUI中,该GUI使用与FXML文件交互的SceneBuilder。 GUI有1-4个按钮,我想要像CodePen上的样式。我不确定如何将HTML / CSS放到适当的位置。

  • 详细说明:

我的JavaFX GUI,包含3个文件。 Main.java,sample.fxml和Controller.java 。我一直在使用SceneBuilder来创建如下所示的GUI。因此,SceneBuilder已将代码编写到sample.fxml文件中。 CodePen代码是纯CSS和HTML,所以我不确定如何将它们集成到我的JavaFX文件中。此CodePen按钮具有简单代码found here.

我已经查阅了JavaFX文档,它给了我像

这样的代码
 Rectangle rect = new Rectangle (100, 40, 100, 100);
            rect.setArcHeight(100);
            rect.setArcWidth(100);
            rect.setFill(Color.BLUE);

            RotateTransition rt = new RotateTransition(Duration.millis(400), rect);
            rt.setByAngle(360);
            rt.setAutoReverse(true);

我已将其更改为一个圆圈,但此代码位于我的Main.java文件中,似乎根本没有解决FXML问题。我见过像

这样编写的JavaFX CSS
-fx-background-color: #7cafc2;
-fx-text-fill: #FFFFFF;
-fx-background-radius: 4;

但这似乎不适用于变换,缩放等。如何将CSS和HTML集成到FXML中以使其与SceneBuilder一致?我只想要4个我的CodePen按钮来替换当前GUI上的按钮1-4。

以下是更多信息,非常感谢那些家伙/女孩

GUI:

GUI

Main.java

import ...

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Form Demo");
        primaryStage.setScene(new Scene(root, 420, 475));
        primaryStage.show();
    }

}

sample.fxml

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

<VBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">

<BorderPane VBox.vgrow="ALWAYS" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <VBox>
         <children>
            <HBox spacing="10.0" VBox.vgrow="ALWAYS">
               <children>
                     <Label fx:id="fileLabel1" prefHeight="33.0" prefWidth="100.0" text="NAS Form:" textOverrun="WORD_ELLIPSIS">
                        <font>
                           <Font name="System Bold" size="17.0" />
                        </font>
                        <padding>
                           <Insets top="7.0" />
                        </padding>
                     </Label>
                     <Label fx:id="fileLabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="33.0" prefWidth="209.0" text="No file selected" HBox.hgrow="ALWAYS">
                        <padding>
                           <Insets top="7.0" />
                        </padding>
                        <font>
                           <Font size="17.0" />
                        </font></Label>
                     <Region nodeOrientation="RIGHT_TO_LEFT" prefHeight="31.0" prefWidth="10.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" onAction="#Browse" prefHeight="31.0" prefWidth="90.0" text="Browse " HBox.hgrow="ALWAYS" />
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </VBox.margin>
            </HBox>
               <Separator prefWidth="200.0" />
            <Region prefHeight="30.0" prefWidth="200.0" />
            <HBox VBox.vgrow="ALWAYS">
               <children>
                  <Region prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" onAction="#FieldData" text="Field Data" HBox.hgrow="ALWAYS" />
                     <Region prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" onAction="#CompData" text="Comp Data" HBox.hgrow="ALWAYS" />
                     <Region prefHeight="200.0" prefWidth="100.0" />
               </children>
            </HBox>
            <HBox VBox.vgrow="ALWAYS">
               <children>
                     <Region prefHeight="200.0" prefWidth="19.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" onAction="#Photos" text="Photos" HBox.hgrow="ALWAYS" />
                  <Region prefHeight="200.0" prefWidth="35.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" onAction="#Sketch" text="Sketch" HBox.hgrow="ALWAYS">
                     <HBox.margin>
                        <Insets />
                     </HBox.margin>
                  </Button>
                     <Region prefHeight="200.0" prefWidth="125.0" />
               </children>
            </HBox>
         </children>
      </VBox>
   </top>
</BorderPane>
</VBox>

1 个答案:

答案 0 :(得分:0)

没有办法将HTML / CSS集成到JavaFX中。

创建hove效果的方法是在代码中创建动画。

创建一个新的类扩展按钮:

public class HoverButton extends Button {

private ScaleTransition scale;
private RotateTransition rotate;
private ParallelTransition transition;

public HoverButton () {
    super();

    createAnimations();
    addEvents();
}

private void createAnimations() {
    scale = new ScaleTransition(Duration.seconds(0.2), this);
    rotate = new RotateTransition(Duration.seconds(0.2), this);

    transition = new ParallelTransition(scale, rotate);
}

private void addEvents() {
    setOnMouseEntered((e) -> {
        transition.stop();
        scale.setToX(1.195);
        scale.setToY(1.195);
        rotate.setToAngle(360);
        transition.play();
    });
    setOnMouseExited((e) -> {
        transition.stop();
        scale.setToX(1);
        scale.setToY(1);
        rotate.setToAngle(0);
        transition.play();
    });

}

}

在你的FXML中添加:

 <?import your.package.HoverButton?>

并用HoverButton替换所有按钮。

如果你想通过CSS控制效果,请看一下。 https://wiki.openjdk.java.net/display/OpenJFX/CSS+API+to+support+custom+UI+Controls