如何将CSS样式表附加到FXML? [英] How to attach a CSS stylesheet to FXML?

查看:542
本文介绍了如何将CSS样式表附加到FXML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循Oracle教程 http://docs.oracle.com/javafx/2 /get_started/fxml_tutorial.htm ,我用片段创建FXML

Following Oracle tutorial http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm, I create FXML with a fragment

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

<?language javascript?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane alignment="CENTER" gridLinesVisible="true" hgap="10.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="fxmlexample.FXMLExampleController" styleClass="root" >
...other stuff...
<stylesheets>
    <URL value="@Login.css" />
</stylesheets>
</GridPane>

NetBeans 8.0对 URL 红色进行着色并说:Class不存在:URL

NetBeans 8.0 colors the URL red and says: "Class does not exist: URL".

编译运行时,JRE 1.8也会抛出异常:

When compiled and run, JRE 1.8 also throws an exception:

...
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: URL is not a valid type.
file:/C:/Users/User1/Documents/NetBeansProjects/FXMLExample/dist/run1970052180/FXMLExample.jar!/fxmlexample/fxml_example.fxml:46

如果我在 JavaFX Scene Builder 1.1中打开fxml_example.fxml文件,它还会抱怨缺少类型,其元素将不会显示:

If I open the fxml_example.fxml file in JavaFX Scene Builder 1.1", it also complains about missing types, elements of which will not be shown:

Missing types are: [URL]

每个人似乎都在使用< URL /> 元素,为什么我不能?

Everyone seems to be using <URL/> elements, why cannot I?

推荐答案

URL java.net中的一个包;你应该从FXML中导入

URL is a class in the java.net package; you should import it from the FXML:

<?import java.net.*?>

这篇关于如何将CSS样式表附加到FXML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆