简体   繁体   English

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

[英]Integrate HTML/CSS into JavaFX's FXML and CSS

  • Short Version: 简洁版本:

My HTML/CSS on CodePen produces a simple button that rotates and grows on hover. 我在CodePen上的HTML / CSS产生了一个简单的按钮,该按钮在悬停时旋转并增长。 I want it integrated into my JavaFX GUI that's using SceneBuilder which interacts with the FXML file. 我希望将其集成到使用SceneBuilder的JavaFX GUI中,该JavaFX GUI与FXML文件进行交互。 The GUI has buttons numbered 1-4 which I want styled like the one on CodePen. GUI有编号为1-4的按钮,我希望它们的样式类似于CodePen上的按钮。 I'm not sure how to get HTML/CSS into the proper place(s). 我不确定如何将HTML / CSS放入适当的位置。

  • Details: 细节:

My JavaFX GUI which consists of 3 files. 我的JavaFX GUI由3个文件组成。 Main.java, sample.fxml, and Controller.java . Main.java,sample.fxml和Controller.java I've been using SceneBuilder to create the GUI shown below. 我一直在使用SceneBuilder创建如下所示的GUI。 SceneBuilder therefore has written code into the sample.fxml file. 因此,SceneBuilder已将代码写入sample.fxml文件。 The CodePen code however is pure CSS and HTML so I'm unsure how to integrate these into my JavaFX files. 但是CodePen代码是纯CSS和HTML,因此我不确定如何将它们集成到JavaFX文件中。 This CodePen button has simple code found here. 此CodePen按钮的简单代码位于此处。

I've consulted the JavaFX docs which gave me code like 我查阅了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);

I've changed it to a circle but this code is in my Main.java file and doesn't seem to address the FXML at all. 我将其更改为一个圆圈,但是此代码在我的Main.java文件中,而且似乎根本没有解决FXML。 I've seen JavaFX CSS written like 我看过JavaFX CSS写成

-fx-background-color: #7cafc2;
-fx-text-fill: #FFFFFF;
-fx-background-radius: 4;

But this doesn't seem to work with transform, scale, etc. How do I integrate the CSS and HTML into the FXML so that its consistent with SceneBuilder? 但这似乎不适用于转换,缩放等。如何将CSS和HTML集成到FXML中,使其与SceneBuilder保持一致? I simply want 4 of my CodePen buttons to replace buttons 1-4 on my current GUI. 我只希望我的CodePen按钮中的4个替换当前GUI上的按钮1-4。

Here is more info, thanks so much guys/gals 这是更多信息,非常感谢你们

GUI: 界面:

图形用户界面

Main.java 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 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>

Theres no way to integrate HTML/CSS into JavaFX. 无法将HTML / CSS集成到JavaFX中。

What you can do the create the hove effect is to create the Animations in your Code. 您可以创建hove效果的方法是在代码中创建动画。

Create a new Class extending Button: 创建一个新的类扩展按钮:

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

}

}

In your FXML add this: 在您的FXML中添加以下内容:

 <?import your.package.HoverButton?>

and replace all Buttons with HoverButton. 并将所有Button替换为HoverButton。

If you want to controlle the effect via CSS take a look at this. 如果要通过CSS控制效果,请看一下。 https://wiki.openjdk.java.net/display/OpenJFX/CSS+API+to+support+custom+UI+Controls https://wiki.openjdk.java.net/display/OpenJFX/CSS+API+to+support+custom+UI+Controls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM