java - 无法添加 CSS 文件

标签 java css netbeans javafx javafx-2

请看一下下面的JavaFX和CSS代码。

Login2.java

package helloworld;

import javafx.application.Application;
import javafx.stage.*;
import javafx.scene.*;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

public class Login2 extends Application
{
    private Text welcome, message;
    private Label userName, password;
    private Button btn;
    private GridPane grid;
    private TextField userNameField;
    private PasswordField passwordField;
    private Scene scene;
    private HBox hbox, hbox2;



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

    @Override
    public void start(Stage stage)
    {
        //Intializing instance Varaibles
        welcome = new Text("Welcome");
        message = new Text();

        userName = new Label("User Name: ");
        password = new Label("Password: ");

        btn = new Button("Submit");
        btn.setOnAction(new Action());

        userNameField = new TextField();
        passwordField = new PasswordField();

        grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setVgap(10);
        grid.setHgap(10);


        //Creating the GUI

        hbox = new HBox();
        hbox.getChildren().add(btn);
        hbox.setAlignment(Pos.BOTTOM_RIGHT);

        hbox2 = new HBox();
        hbox2.getChildren().add(message);
        hbox2.setAlignment(Pos.BOTTOM_RIGHT);

        grid.add(welcome,0,0);
        grid.add(userName,0,1);
        grid.add(userNameField,1,1);
        grid.add(password,0,2);
        grid.add(passwordField,1,2);
        grid.add(hbox,1,3);
        grid.add(hbox2,1,4);



        scene = new Scene(grid,300,275);


        stage.setTitle("Welcome Form");
        stage.setScene(scene);

        scene.getStylesheets().add(Login2.class.getResource("Login.css").toExternalForm());
        stage.show();
    }

    private class Action implements EventHandler<ActionEvent>
    {
        public void handle(ActionEvent ae)
        {
            message.setFill(Color.CRIMSON);
            message.setText("You pressed the button");
        }
    }
}

Login.css

/* 
    Document   : Login
    Created on : Jul 14, 2012, 8:04:31 PM
    Author     : Yohan
    Description:
        Purpose of the stylesheet follows.
*/

.root {

   -fx-background-image: url(Desert.jpg);
}

当我运行此程序时,出现以下错误。

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source) at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source) at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source) at java.lang.Thread.run(Thread.java:722) Caused by: java.lang.NullPointerException at helloworld.Login2.start(Login2.java:80) at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source) at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source) ... 1 more Java Result: 1

上传的图片显示了我的文件夹结构。

为什么我会收到此错误?我不明白!无论如何,这是我的第三个 JavaFX 代码。请帮忙!

enter image description here

最佳答案

在Netbeans中,使用默认的元素设置,大多数时候当非java资源文件被添加到包结构中时,元素需要从头开始重建。这样,新资源将被复制到“build”文件夹,其中存储和运行已编译的 java 文件(没有 NullPointerException)。可以通过右键单击元素并执行“清理并构建”来完成重建。

关于java - 无法添加 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484745/

相关文章:

java - 每 1 分钟在 for 循环中调用一个命令

java - Eclipse 将(工作)代码标记为编译错误并且不会运行

jquery - 被 Div 隐藏的下拉菜单

javascript - Html 中背景的正文

java - 使用 Net Beans 创建 JAR 文件,包括所有依赖库和文件夹

java - Servlet 初学者

java - org.glassfish.javaeetutorial 的不可解析父 POM :dukes-tutoring

java Nashorn ReferenceError

java - JPA 实体创建 - 对象与 "Primitives"

css - Safari 中未加载背景图片