在尝试将jss用于javafx时,为什么会出现InvocationTargetException?

时间:2016-12-08 21:21:13

标签: java css user-interface javafx

我是javafx的新手,我正在尝试将我的类连接到我的CSS文件,但是当我使用时:

scene.getStylesheets().add("Viper.css");

我收到以下警告:

Dec 08, 2016 9:12:54 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "Viper.css" not found.

但是当我使用时:

scene.getStylesheets().add(getClass().getResource("/resources/CSS/Viper.css").toExternalForm());

我收到一个InvocationTargetException

这是我的全班,我很肯定文件路径是正确的。我正在使用NetBeans IDE。

package com.GUI;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class window extends Application{


    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("OmegaBrain");

        //Create Panes
        Pane titlePane = new Pane();


        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        Text sceneTitle = new Text("Welcome To OmegaBrain");
        sceneTitle.setFont(Font.font("Helvetica", FontWeight.NORMAL, 20));
        grid.add(sceneTitle, 0, 0, 4, 1);



        Scene scene = new Scene(grid, 300, 275);
        scene.getStylesheets().add("/resources/CSS/Viper.css");


        primaryStage.setScene(scene);
        primaryStage.show();


        Button play = new Button("Play");
        grid.add(play, 1, 1);

        Button leaderboard = new Button("Leaderboard");
        grid.add(leaderboard, 2, 1);

        Button faq = new Button("FAQs");
        grid.add(faq, 3, 1);

        Button exit = new Button("Exit");
        grid.add(exit, 4, 1);


        play.setOnAction((ActionEvent e) -> {
         System.out.println("The play button was clicked!");
        });



    }


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



}

2 个答案:

答案 0 :(得分:0)

假设您的应用程序是使用此层次结构构建的:

Application ->src -->com/GUI/window.java -->resources/CSS/Viper.css

然后这段代码应该可以工作:

scene.getStylesheets().add(getClass().getResource("/resources/CSS/Viper.css").toExternalForm());
// or
scene.getStylesheets().add("/resources/CSS/Viper.css");

答案 1 :(得分:0)

您可以使用Option Explicit Sub HighlightMatches() Application.ScreenUpdating = False 'Declare variables Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean 'Set up the count as the number of filled rows in the first column of Sheet1. iRowL = Cells(Rows.Count, 1).End(xlUp).Row 'Cycle through all the cells in that column: For iRow = 1 To iRowL 'For every cell that is not empty, search through the first column in each worksheet in the 'workbook for a value that matches that cell value. If Not IsEmpty(Cells(iRow, 1)) Then For iSheet = ActiveSheet.Index + 1 To Worksheets.Count bln = False var = Application.Match(Cells(iRow, 1).Value, Worksheets(iSheet).Columns(1), 0) 'If you find a matching value, indicate success by setting bln to true and exit the loop; 'otherwise, continue searching until you reach the end of the workbook. If Not IsError(var) Then bln = True Exit For End If Next iSheet End If 'If you do not find a matching value, do not bold the value in the original list; 'if you do find a value, bold it. If bln = False Then Else Cells(iRow, 1).Interior.ColorIndex = 9 'here should be the conditional if End If Next iRow Application.ScreenUpdating = True End Sub

解决此问题