英文:
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
及其子类,以及ImageView
、WebView
和MediaView
具有默认样式类。)因此,选择器不会选择该线条。你可以在代码中明确地为线条添加样式类:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论