【问题标题】:Unable to add the CSS file无法添加 CSS 文件
【发布时间】:2012-07-14 03:30:07
【问题描述】:

请看下面的 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);
}

当我运行它时,我收到以下错误。

应用程序启动方法中的异常线程“main”中的异常 java.lang.RuntimeException:应用程序启动方法中的异常 com.sun.javafx.application.LauncherImpl.launchApplication1(未知 来源)在 com.sun.javafx.application.LauncherImpl.access$000(未知 来源)在 com.sun.javafx.application.LauncherImpl$1.run(未知 来源)在 java.lang.Thread.run(Thread.java:722) 引起: java.lang.NullPointerException 在 helloworld.Login2.start(Login2.java:80) 在 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) 在 com.sun.glass.ui.win.WinApplication.access$100(来源不明)在 com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source) ... 1 更多 Java 结果:1

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

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

【问题讨论】:

  • 您是否尝试过右键单击项目并执行“清理并构建”?
  • 哇。现在它正在工作。我不知道为什么即使在进行了多次清理和构建之后它也没有响应。非常感谢。请提供您的答案作为“答案”。然后我可以解决它:)
  • 如果您正在使用工具栏按钮/快捷方式并且另一个项目是“设置主项目”(如您发布的图片所示),那么您可能正在构建另一个项目。

标签: java css netbeans javafx javafx-2


【解决方案1】:

我不得不承认包结构有点棘手,因为很容易忘记它是如何完成的,并且必须花一个小时在线阅读示例才能使您的必要结构发挥作用。

我会尝试以下方法。创建一个名为 helloworld.support(或其他合适的名称)的新包,然后将 CSS 文件移到那里。然后你应该可以通过调用来加载它:

Login2.class.getResource("/helloworld/support/Login.css")

(为了清楚起见,删除了周围的其余部分)。

我将 CSS 文件从源代码包中移出的原因是几个月前我意识到每次我使用“Clean & Build”(在 Netbeans 7 中)时,它都会从我的源代码中删除所有非 Java 文件包,所以我所有的图像都丢失了,我不得不再次将它们移回。一旦我给图像提供了他们自己的包,Netbeans 就不理会它们了。

【讨论】:

  • 感谢您的回复:)。但是,我从来没有遇到过你提到的问题。我在 Swing 中清理和构建了很多次,其中有时包含 20 多个图像,但我从未丢失过任何图像!
【解决方案2】:

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

【讨论】:

  • 感谢 Uluk 非常感谢您的帮助 :)
猜你喜欢
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多