如何使用外部css文件来设置javafx应用程序的样式 [英] How to use external css file to style a javafx application
问题描述
我想用外部css文件设置我的javafx应用程序样式,但是当我添加css文件时,它没有产生任何差异。我正在使用Netbeans 7.4 IDE和jdk8,虽然代码没有指出任何错误或异常,但我没有得到所需的输出。我完全糊涂了该做什么。我的代码是......
i want to style my javafx application with an external css file but when i am adding css file, it is not creating any difference. i am using Netbeans 7.4 IDE and jdk8, although the code is not pointing any error or exception but i am not getting the required output. I am totaly confused what to do. My code is...
package manualstyle;
import java.io.File;
import java.net.URL;
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.stage.Stage;
/**
*
* @author vickyjonnes
*/
public class ManualStyle extends Application {
@Override
public void start(Stage primaryStage) {
Group root=new Group();
Scene scene = new Scene(root, 300, 250);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setLayoutX(100);
btn.setLayoutY(100);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
root.getChildren().add(btn);
primaryStage.setScene(scene);
String css = ManualStyle.class.getResource("myStyle.css").toExternalForm();
scene.getStylesheets().add(css);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
我有一个css文件,它位于同一目录中和css文件的内容是:
I have a css file which resides in the same directory and the content of css file is :
.root{
-fx-background-color: #ff0066;
}
推荐答案
请通过以下解决方案。如果您仍然遇到问题,请与我们联系:
Please go through the following solution. Let me know, if you still face issues :
https:// stackoverflow .com / a / 22048338/1759128
这篇关于如何使用外部css文件来设置javafx应用程序的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!