.javaFXApplet(未知来源)ImageView

时间:2015-12-20 10:34:21

标签: java javafx embedded-resource fxml

请帮助我,当我在浏览器中运行程序时它会返回此错误:

Image img = new Image(getClass().getResource("u.png").toExternalForm());

此行发生错误:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;


public class E1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField UserF = new TextField();
        UserF.setPromptText("nom d'utilisateur");
        UserF.setLayoutX(50);
        Image img = new Image(getClass().getResource("u.png").toExternalForm()); // error occurs here
       ImageView pic = new ImageView(img);
        pic.setLayoutX(-100);
        pic.setLayoutY(-60);
        PasswordField  PassF = new PasswordField ();
        PassF.setPromptText(" mot de passe");
        PassF.setLayoutX(50);
        PassF.setLayoutY(50);

        Button login = new Button("Login");
        login.setLayoutX(100);
        login.setLayoutY(100);
       // btn.setMaxSize(100, 200);

       BorderPane Menu= new BorderPane(); 
       Group root = new Group();

       root.getChildren().addAll(UserF,PassF,login,pic);
       Menu.setCenter(root);
       Menu.setStyle("-fx-background-color: linear-gradient(from 25% 25% to 100% 100%,  #6db3f2 , #1e69de)");

        Scene scene;
        scene= new Scene(Menu);


    //    primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

这是完整的代码:

Sub Test()
Dim DiffSh1() As Variant
Dim DiffSh2() As Variant
Call Compare_Sheets(ThisWorkbook.Sheets("Sheet1"), ThisWorkbook.Sheets("Sheet2"), DiffSh1, DiffSh2)
'Now you can use the values in the two arrays as you need
For x = LBound(DiffSh1, 1) To UBound(DiffSh1, 1)
    For y = LBound(DiffSh1, 2) To UBound(DiffSh1, 2)
    If DiffSh1(x, y) <> "" Then
        MsgBox ("Cell at Row " & x & " Column " & y & " isn't equal:" & vbCrLf & _
            "Value in sheet1 is: " & DiffSh1(x, y) & vbCrLf & _
            "Value in sheet2 is: " & DiffSh2(x, y))
    End If
    Next y
Next x
End Sub

Public Sub Compare_Sheets(ByVal Sh1 As Worksheet, ByVal Sh2 As Worksheet, ByRef DiffIn1() As Variant, ByRef DiffIn2() As Variant)
    Dim LastCol
    Dim LastRow
    LastCol = Sh1.Cells(1, 1).SpecialCells(xlLastCell).Column
    If Sh2.Cells(1, 1).SpecialCells(xlLastCell).Column > LastCol Then
        LastCol = Sh2.Cells(1, 1).SpecialCells(xlLastCell).Column
    End If
    LastRow = Sh1.Cells(1, 1).SpecialCells(xlLastCell).Row
    If Sh2.Cells(1, 1).SpecialCells(xlLastCell).Row > LastRow Then
        LastRow = Sh2.Cells(1, 1).SpecialCells(xlLastCell).Row
    End If
ReDim DiffIn1(1 To LastRow, 1 To LastCol)
ReDim DiffIn2(1 To LastRow, 1 To LastCol)
Dim mCol As Long, mRow As Long
For mCol = 1 To LastCol
    For mRow = 1 To LastRow
        If Sh1.Cells(mRow, mCol) <> Sh2.Cells(mRow, mCol) Then
            DiffIn1(mRow, mCol) = Sh1.Cells(mRow, mCol).Value
            DiffIn2(mRow, mCol) = Sh2.Cells(mRow, mCol).Value
        Else
            DiffIn1(mRow, mCol) = ""
            DiffIn2(mRow, mCol) = ""
        End If
    Next mRow
Next mCol
End Sub

1 个答案:

答案 0 :(得分:0)

立即行动,通过以下方式解决:

替换

Image img = new Image(getClass().getResource("u.png").toExternalForm());

by:

Image img = new Image(getClass().getResourceAsStream("/icons/u.png"));