我想使用外部 css 文件来设计我的 javafx 应用程序,但是当我添加 css 文件时,它不会产生任何差异。我正在使用 Netbeans 7.4 IDE 和 jdk8,虽然代码没有指出任何错误或异常,但我没有得到所需的输出。我完全困惑该怎么办。我的代码是...
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 文件的内容是:
.root{
-fx-background-color: #ff0066;
}
最佳答案
请执行以下解决方案。如果您仍然遇到问题,请告诉我:
关于java - 如何使用外部 css 文件来设计 javafx 应用程序的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22066799/