Application start方法文件上传中的JavaFX异常

时间:2017-04-26 18:06:56

标签: image file javafx

高我已经尝试了两天来创建一个卡片夹的链接,我目前正试图获得一个随机卡添加到界面但我继续得到一个异常我不明白我的课程如何路径是错的我尝试了一切任何帮助将非常感激。我有点死路一条,我找不到任何有助于解决这个问题的事情 我的代码如下

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
//Imports for components in this application.
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
//Imports for images.
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HighLowJavafx extends Application {
    // Declare labels
    Label lblFirst, lblSecond, lblNext;

    // Declare Radio Buttons

    // radio button 1 with an empty string for its label
    RadioButton rb1 = new RadioButton();

    // radio button 2 with an empty string for its label
    RadioButton rb2 = new RadioButton();

    Button btnFirst, btnSecond;

    // Declare controls at class scope.
    Label lblStatus;
    MenuBar mBar;

    Image imgRight2;
    ImageView imgViewRight;

    public HighLowJavafx() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init() {

        // Instantiate components.
        lblFirst = new Label("First Card Dealt:");
        lblSecond = new Label("Second Card dealt:");
        lblNext = new Label("Next Card Will Be:");

        mBar = new MenuBar();
        // ------------------------------------------------------------------
        // File menu
        Menu mnuFile = new Menu("File");

        // add new game to file menu
        MenuItem newGame = new MenuItem("New Game");
        mnuFile.getItems().add(newGame);

        // add shuffle to file menu
        MenuItem shuffle = new MenuItem("Shuffle");
        mnuFile.getItems().add(shuffle);

        // add exit to file menu
        MenuItem exit = new MenuItem("Exit");
        mnuFile.getItems().add(exit);

        // Add the menu to the menu bar.
        mBar.getMenus().add(mnuFile);

        // -------------------------------------------------------------------
        // Create a help menu.
        Menu mnuHelp = new Menu("Help");

        // Add menu items to the help menu.
        MenuItem aboutItem = new MenuItem("About");
        mnuHelp.getItems().add(aboutItem);

        aboutItem.setOnAction(ae -> showAboutDialog());

        // Add the help menu to the menu bar.
        mBar.getMenus().add(mnuHelp);

        // ------------------------------------------------------------------

        // Add image view

        imgViewRight = new ImageView();

        // Handle menu events.
        newGame.setOnAction(ae -> {

        });

    }// init()

    private void showAboutDialog() {

        // Create a new stage and set sizes and title.
        Stage dialog = new Stage();
        dialog.setWidth(250);
        dialog.setHeight(180);
        dialog.setTitle("About");

        // A label for the dialog.
        Label lblAbout = new Label(" Luke Gallagher 2933229 ");

        // An OK button for the dialog.
        Button btnOK = new Button("OK");

        // Handle events (clicks) on the dialog button.
        btnOK.setOnAction(ae -> dialog.close());
        btnOK.setMinWidth(120);

        // Layout for the about dialog.
        VBox vbAbout = new VBox();
        BorderPane dlgBP = new BorderPane();
        dlgBP.setCenter(btnOK);

        // The button.
        BorderPane bpLbl = new BorderPane();
        bpLbl.setCenter(lblAbout);

        // Set the padding.
        vbAbout.setPadding(new Insets(40));
        vbAbout.setSpacing(20);

        // Add components.
        vbAbout.getChildren().add(bpLbl);
        vbAbout.getChildren().add(dlgBP);

        // Create a scene.
        Scene dlgScene = new Scene(vbAbout);

        // Set the scene.
        dialog.setScene(dlgScene);

        // Show the stage.
        dialog.show();

    }// showAboutDialog()

    public int cardNum() {
        int Num = (int) Math.ceil(Math.random() * 52);
        return Num;
    }

    @Override
    public void start(Stage pStage) throws Exception {

        // Set the title.
        pStage.setTitle("Hi-Lo Card Game");

        // Set the width and height.
        // Width and height.
        pStage.setWidth(600);
        pStage.setHeight(400);

        // images
        // fileR = new File("cards/" + cardRight.toString(cardRight)+/".png")
        // imgRight2= new Image("cards/2_of_clubs.png");

        // imgViewRight.setImage(imgRight2);

        // imgRight2= new Image(fileR.toURI().toString());

        // imgViewRight.setImage(imgRight2);

        // Instantiate components.
        lblFirst = new Label("First Card Dealt:");
        lblSecond = new Label("Second Card Dealt:");
        lblNext = new Label("Next Card Will Be:");

        // Start and stop buttons.
        btnFirst = new Button("<- Deal First Card");
        btnSecond = new Button("Deal Second Card ->");

        // Manage button sizes.
        btnFirst.setMaxWidth(130);
        btnSecond.setMaxWidth(130);

        // rb button functionality only one can be selected at once
        ToggleGroup group = new ToggleGroup();
        RadioButton rb1 = new RadioButton("Higher");
        rb1.setToggleGroup(group);
        rb1.setSelected(true);
        RadioButton rb2 = new RadioButton("Lower");
        rb2.setToggleGroup(group);

        // Create a layout.
        BorderPane bpMain = new BorderPane();

        // Add components to the layout.

        bpMain.setTop(mBar);

        BorderPane.setAlignment(lblSecond, Pos.TOP_RIGHT);
        BorderPane.setMargin(lblSecond, new Insets(12, 80, 12, 12));
        bpMain.setRight(lblSecond);

        VBox pane1 = new VBox();
        pane1.getChildren().add(lblNext);
        pane1.getChildren().add(rb1);
        pane1.getChildren().add(rb2);
        pane1.getChildren().add(btnFirst);
        pane1.getChildren().add(btnSecond);
        pane1.setSpacing(10);

        BorderPane.setAlignment(pane1, Pos.CENTER);
        BorderPane.setMargin(pane1, new Insets(40, 12, 12, 120));
        bpMain.setCenter(pane1);

        // images

        // final Image img = new Image("cards/"+cardNum()+".png");
        final ImageView imgView = new ImageView(".cards");

        // vbox2
        VBox pane2 = new VBox();

        pane2.getChildren().add(lblFirst);

        BorderPane.setAlignment(pane2, Pos.TOP_LEFT);
        BorderPane.setMargin(pane2, new Insets(12, 12, 12, 12));
        bpMain.setLeft(pane2);
        // disables max button to keep everything in-line
        pStage.resizableProperty().setValue(Boolean.FALSE);

        // Create a scene.
        Scene s = new Scene(bpMain);

        // Set the scene.
        pStage.setScene(s);

        btnFirst.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                imgView.setImage(new Image("cards/2_of_clubs.png"));

                pane2.getChildren().add(imgView);
                pane2.getChildren().add(lblFirst);

                BorderPane.setAlignment(pane2, Pos.TOP_LEFT);
                BorderPane.setMargin(pane2, new Insets(12, 12, 12, 12));
                bpMain.setLeft(pane2);
            }
        });

        // Show the stage.
        pStage.show();

    }// start()

    public static void main(String[] args) {
        // Launch the application.
        launch();
    }// main()

}// class

我得到的例外和错误如下

   Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1118)
    at javafx.scene.image.Image.<init>(Image.java:620)
    at javafx.scene.image.ImageView.<init>(ImageView.java:166)
    at HighLowJavafx.start(HighLowJavafx.java:267)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
    ... 12 more
Exception running application HighLowJavafx
Picked up _JAVA_OPTIONS: -Xmx512M

0 个答案:

没有答案