将HTML/CSS集成到JavaFX';s FXML和CSS 简短版本:
我在CodePen上的HTML/CSS生成了一个简单的按钮,在悬停时旋转并增长。我希望它集成到我的JavaFXGUI中,该GUI使用与FXML文件交互的SceneBuilder。GUI有编号为1-4的按钮,我想将其样式设置为CodePen上的按钮。我不知道如何将HTML/CSS放到正确的位置将HTML/CSS集成到JavaFX';s FXML和CSS 简短版本:,java,html,css,javafx,fxml,Java,Html,Css,Javafx,Fxml,我在CodePen上的HTML/CSS生成了一个简单的按钮,在悬停时旋转并增长。我希望它集成到我的JavaFXGUI中,该GUI使用与FXML文件交互的SceneBuilder。GUI有编号为1-4的按钮,我想将其样式设置为CodePen上的按钮。我不知道如何将HTML/CSS放到正确的位置 详情: 我的JavaFXGUI由3个文件组成。Main.java、sample.fxml和Controller.java。我一直在使用SceneBuilder创建如下所示的GUI。因此,SceneBui
- 详情:
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。我见过JavaFXCSS像这样写
-fx-background-color: #7cafc2;
-fx-text-fill: #FFFFFF;
-fx-background-radius: 4;
但这似乎不适用于转换、缩放等。如何将CSS和HTML集成到FXML中,使其与SceneBuilder保持一致?我只想用4个CodePen按钮替换当前GUI上的按钮1-4
这里有更多信息,非常感谢各位
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>
无法将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?>
并将所有按钮替换为鼠标悬停按钮
如果你想通过CSS控制效果,看看这个。
可以通过webview类将html添加到JavaFx应用程序中。请检查,但这不是很实际,只为css动画做。