无法添加CSS文件
请看下面的JavaFX和CSS代码。无法添加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);
}
当我运行它,我收到以下错误。
在例外申请开始方法中的异常线程“主” 了java.lang.RuntimeException:异常在在COM com.sun.javafx.application.LauncherImpl.launchApplication1(未知 源)申请开始方法。 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) : 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) com.sun.glass.ui.win.WinApplication.access $ 100(Unknown Source)at com.sun.glass.ui.win.WinApplication $ 2 $ 1.run(Unknown Source)... 1 更多Java结果:1
上传的图像显示我的文件夹结构。
为什么我得到这个错误?我无法理解!无论如何,这是我的第三个JavaFX代码。请帮忙!
在具有默认项目设置的Netbeans中,大部分时间将非java资源文件添加到包结构中时,项目需要从头开始重新构建。这样,新的资源将被复制到编译后的java文件存储和运行的“build”文件夹中(没有NullPointerException)。重建可以通过右键单击项目并执行“清理和构建”来完成。
谢谢乌鲁克真的很感谢帮助:) – 2012-07-14 20:07:24
我不得不承认,封装结构是有点棘手,因为它太容易忘记它是如何做,并不得不花费在网上,让您必要的结构上班一个小时的阅读的例子。
我会尝试以下操作。创建一个名为helloworld.support
(或其他合适的名称)的新包,然后将CSS文件移动到那里。然后,您应该可以通过以下方式加载它:
Login2.class.getResource("/helloworld/support/Login.css")
(为了清楚起见,删除了其余的声明)。
我将CSS文件移出源码包的原因是我几个月前意识到每当我使用“Clean & Build”(在Netbeans 7中)时,它都会删除我的任何非Java文件源包,所以我的所有图像都丢失了,我不得不将它们移回去。一旦我给了他们自己的软件包Netbeans让他们独自一人。
感谢您的回复:)。但是,我从来没有面对过你提到的问题。我在Swing中有很多次清理和构建,其中有时包含超过20个图像,但我从来没有丢过任何东西! – 2012-07-14 15:45:59
您是否尝试过右键单击该项目并执行“清理和构建”? – 2012-07-14 15:34:43
哇。现在它正在工作。我不知道为什么即使在完成一些清理和构建之后它也没有响应。非常感谢。请提供您的答案作为“答案”。然后我可以把它解决:) – 2012-07-14 15:44:12
也许你正在建设另一个项目,如果你正在使用工具栏按钮/快捷方式,如果另一个项目是“设置一个主项目”(如在你张贴的图片中看到)。 – 2012-07-14 16:19:27