【问题标题】:javaFX: styleClass is not working in jar filejavaFX:styleClass 在 jar 文件中不起作用
【发布时间】:2013-10-23 08:23:36
【问题描述】:

当我直接从 Eclipse 运行应用程序时,它工作正常。但在使用 ant 构建应用程序后,生成的 jar 文件无法正常工作。

但是,如果我从 fxml 中删除 Styleclass="theme" 它也可以使用 jar 文件正常工作

Java 文件

public class MainClass extends Application {


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


@Override
public void start(Stage stage) throws Exception {
Parent root =   FXMLLoader.load(getClass().
getResource("/resources/fxmlDocument/Sample.fxml"));


stage.setTitle("SAMPLE");
Scene scene= new Scene(root);

scene.getStylesheets().add(getClass().getClassLoader().
getResource("resources/css/sample.css").toExternalForm());


stage.setScene(scene);
stage.setResizable(false);

stage.show();
}
}

Fxml 文件:

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

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="theme" xmlns:fx="http://javafx.com/fxml" fx:controller="com.integra.test.MainClassController">
  <children>
    <Label layoutX="300.0" layoutY="184.0" text="Great..!" />
    <Button fx:id="clickbutton" layoutX="207.0" layoutY="184.0" mnemonicParsing="false" text="clickme" />
    <TextField fx:id="text1" layoutX="207.0" layoutY="220.0" prefWidth="200.0" />
    <Button fx:id="closebutton" layoutX="459.0" layoutY="318.0" mnemonicParsing="false" onAction="#closeButtonAction" prefHeight="37.0" prefWidth="75.0" text="Close" />
  </children>
</AnchorPane>

【问题讨论】:

  • 请展示如何在应用程序中将 css 添加到场景中。并确保您“清理并构建”您的项目,以确保非 java 文件在 jar 中正确更新。
  • 我也添加了代码,你可以看看。
  • 你使用的是getClass().getResource()和getClass().getClassLoader().getResource(),有什么不一致的原因吗?
  • 不,我只是在以不同的方式尝试它。它会导致任何问题吗?

标签: css javafx-2 fxml


【解决方案1】:

您还可以使用它来向控件添加样式表。

control.getStylesheets().add
 (MainClass.class.getResource("Sample.css").toExternalForm());

确保 css 文件在同一个包中或指定源路径。

第二种方法是使用场景构建器并使用场景构建器添加css。你会得到这个教程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2017-08-01
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多