JavaFX项目的单独CSS文件

huangapple go评论63阅读模式
英文:

Separate css file for JavaFX project

问题

以下是要翻译的内容:

如何在一个独立的CSS文件中为JavaFX项目的所有线条对象应用特定的样式?这是Java代码:

package com.example.demo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        Pane root = new Pane();
        Scene scene = new Scene(root, 1200, 800);
        stage.setTitle("Simulink viewer");
        Label l1 = new Label("hdkhsd");
        l1.setLayoutX(100);
        l1.setLayoutY(100);
        Label l2 = new Label("helle");
        l2.setLayoutX(400);
        l2.setLayoutY(400);
        root.getChildren().addAll(l1, l2);
        Line l = new Line(10, 10, 90, 90);
        root.getChildren().add(l);
        scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }

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

这是CSS文件:

.root {
    -fx-background-color: #ADD8E6;
}

.label {
    -fx-border-color: red;
}

.line {
    -fx-stroke: green;
}

这个样式对根(root)和标签(label)都起作用,但对线条(line)不起作用。问题在哪里?

我收到了以下警告:

May 13, 2023 4:31:05 PM javafx.css.CssParser declaration WARNING: CSS Error parsing file:/E:/College/Now/Advanced/Projects/normal%20java%20projects/demo/target/classes/com/example/demo/style.css: Expected COLON at [10,11]

我实际上读到线条的样式类默认为空。这是什么意思?这是否意味着我不能在独立的CSS文件中为线条设置样式?

英文:

How can I apply a certain style for all lines objects in a separate css file for a javaFX project? This is the java code :

package com.example.demo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        Pane root = new Pane();
        Scene scene = new Scene(root, 1200, 800);
        stage.setTitle("Simulink viewer");
        Label l1 = new Label("hdkhsd");
        l1.setLayoutX(100);
        l1.setLayoutY(100);
        Label l2 = new Label("helle");
        l2.setLayoutX(400);
        l2.setLayoutY(400);
        root.getChildren().addAll(l1,l2);
        Line l = new Line(10,10,90,90);
        root.getChildren().add(l);
        scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }

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

And this is the css file:

.root{
-fx-background-color : #ADD8E6;
}

.label {
-fx-border-color: red;
}

.line {
-fx-stroke = green;

}

The style works for both root and label, but it doesn't work for line. What is the problem?

I'm having this warning:

>May 13, 2023 4:31:05 PM javafx.css.CssParser declaration
WARNING: CSS Error parsing file:/E:/College/Now/Advanced/Projects/normal%20java%20projects/demo/target/classes/com/example/demo/style.css: Expected COLON at [10,11]

I actually read that style class for line is empty by default. What does that mean? Does it mean I can't style line in a separate css file?

答案1

得分: 5

请注意,你的CSS文件中有一个语法错误:

-fx-stroke = green;

应该是

-fx-stroke: green;

此外,请注意从文档中了解到,Line的样式类默认为空。(只有Control及其子类,以及ImageViewWebViewMediaView具有默认样式类。)因此,选择器不会选择该线条。你可以在代码中明确地为线条添加样式类:

Line l = new Line(10,10,90,90);
l.getStyleClass().add("line");

或者使用类型选择器。根据文档:

Node的getTypeSelector方法返回一个类似于CSS类型选择器的字符串。默认情况下,此方法返回类的简单名称。

因此,以下内容将在不在Java代码中添加样式类的情况下工作:

Line {
-fx-stroke: green;
}
英文:

First, note you have a syntax error in your CSS file:

-fx-stroke = green;

should be

-fx-stroke: green;

Additionally, note from the documentation that the style class of Line is empty by default. (Only Control and its subclasses, along with ImageView, WebView, and MediaView have default style classes.) Consequently, the selector will not select the line. You can either add the style class to the line explicitly:

    Line l = new Line(10,10,90,90);
    l.getStyleClass().add("line");

or us a Type Selector. From the documentation:

>Node's getTypeSelector method returns a String which is analogous to a CSS Type Selector. By default, this method returns the simple name of the class.

So the following will work without adding the style class in the Java code:

Line {
-fx-stroke: green;

}

huangapple
  • 本文由 发表于 2023年5月13日 22:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定