JavaFx我的css文件无效 [英] JavaFx my css file is not working
问题描述
它工作得比较早,但现在这里是一个截图
It was working earlier but now here is a screenshot
我正在运行的程序,显然不是。有人可以帮我这个吗?我是JavaFX的初学者。
of my running program and it is clearly not. Can somebody help me with this? I am a beginner to JavaFX.
Analog_clock.java
package analog_clock;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
*
* @author ishansrivastava
*/
public class Analog_clock extends Application {
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle();
circle.setCenterX(100.0f);
circle.setCenterY(100.0f);
circle.setRadius(50.0f);
Group g = new Group();
g.getChildren().add(circle);
Pane bg = new Pane();
//bg.setBackground(new Background(new BackgroundFill("-fx-color: #ACACE6", null,null)));
bg.getChildren().add(g);
Scene scene = new Scene(bg, 300, 250);
scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show()cl;
}
public static void main(String[] args) {
launch(args);
}
}
style.css
.Circle
{
-fx-stroke: #757575; /* sonic silver */
-fx-fill: #00CC99; /* caribbean green */
}
.pane
{
-fx-background-color: #ACACE6; /* maximum blue purple */
}
感谢您帮助我。
更新:
将我的css文件更改为:
after changing my css file to this :
.circle
{
-fx-stroke: #757575; /* sonic silver */
-fx-fill: #00CC99; /* caribbean green */
}
.root
{
-fx-background-color: #ACACE6; /* maximum blue purple */
}
我的背景似乎是紫色的,因为我在我的代码中没有任何名为 root 的内容。
my background appears to be purple where as I have nothing named root in my code.
推荐答案
如果设置 id
>圈& bg
,它应该有效:
If you set the id
of the circle
& bg
, it should work:
circle.setId("circle");`
bg.setId("bg");
然后创建 CSS
文件:
#circle{
-fx-stroke: #757575; /* sonic silver */
-fx-fill: #00CC99; /* caribbean green */
}
#bg{
-fx-background-color: #ACACE6; /* maximum blue purple */
}
关于 root
这个词,我不太确定,但我认为它有一个JavaFX中的特殊含义(特别是 FXML
语言),这就是为什么它被理解为你定义了 Pane bg
用这个名字!但是,请查看如何理解和使用< fx:root>
在JavaFX中?它可能有所帮助。
And regarding the root
word, I'm not quite sure, but I think it has a special meaning in JavaFX (specifically FXML
language), and that's why it has been understood as if you defined your Pane bg
with that name! However, have a look at How to understand and use <fx:root>
in JavaFX? it may help.
这篇关于JavaFx我的css文件无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!