洗牌的程序,当我按下随机播放按钮时,卡片会在GUI窗口中消失

时间:2014-08-30 02:45:03

标签: javafx

到目前为止,我的所有代码编译得很好,但是随机播放按钮会使所有卡片消失

import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.control.Button;
import javafx.geometry.Pos;

public class P6 extends Application
{
    public static final int ASIZE = 54;
    public static final int ROWS = 6;
    public static final int COLS = 9;

    public void start(Stage primaryStage)
    {
        int aCardDeck[] = new int[ASIZE];       //54 cards in a deck
        for( int i = 0 ; i < ASIZE ; i++ )  //ASIZE is 54
        aCardDeck[i] = i + 1;       //populate with elements 1-54

        GridPane gPane = new GridPane();        //Instantiate Grid for cards in rows/cols

        for( int i = 0, k = 0 ; i < ROWS && k < ASIZE; i++ )  //6 rows, 9 columns 
            for( int j = 0; j < COLS ; j++ )
            gPane.add(new ImageView("image/card/" + aCardDeck[k++] + ".png"),j,i);



        Button btShuffle = new Button("Shuffle");
        btShuffle.setOnAction
        ( e ->                  // Lamda Event Handler
        {                   // Like anonymous inner class
        int r, c, n;
        shuffle( aCardDeck );       // Random shuffle
        gPane.getChildren().clear();
        for ( r = n = 0; r < ROWS && n < ASIZE; r++)
        for (c = 0; c < COLS ; c++)
        gPane.add(new ImageView("~/../public/image/card/" + aCardDeck[n++]
        + ".png"),c,r);
        }
        ); 

        HBox hBox = new HBox(5);            // Row of buttons
        hBox.getChildren().add( btShuffle );    // Add button to box


        BorderPane pane = new BorderPane();
        pane.setCenter(gPane);          // Layout rows of cards in center
        pane.setBottom(hBox);           // Layout buttons at bottom
        BorderPane.setAlignment(hBox, Pos.CENTER);
        Scene scene = new Scene(pane, 650, 600); // Create scene, place in stage
        primaryStage.setTitle("P6");        // Set the stage title
        primaryStage.setScene(scene);       // Place the scene in the stage
        primaryStage.show();            // Display the stage
        }

    public void ascendSort (int [] a){}
    public void descendSort (int [] a){}
    public void ascendRankSort (int [] a){}
    public void descendRankSort (int [] a){}

    public void shuffle( int [] a ){
        int r, tmp;
        Random rand = new Random();
        long seed = System.currentTimeMillis();
        rand.setSeed(seed);
        for( int i = 0 ; i < ASIZE ; i++ ){
        r = rand.nextInt(ASIZE);

        tmp = a[r];
        a[r] = a[i];
        a[i] = tmp; 
}
}
    public static void main(String[] arg`enter code here`s)
    {
    launch(args);
    }
}

错误消息: 线程“JavaFX Application Thread”中的异常java.lang.IllegalArgumentException:无效的URL:找不到无效的URL或资源     在javafx.scene.image.Image.validateUrl(Image.java:1094)     在javafx.scene.image.Image。(Image.java:598)     在javafx.scene.image.ImageView。(ImageView.java:164)     在P6.lambda $开始$ 0(P6.java:39)     在P6 $$ Lambda $ 1 / 16273898.handle(未知来源)     at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)     在javafx.event.Event.fireEvent(Event.java:204)     在javafx.scene.Node.fireEvent(Node.java:8175)     在javafx.scene.control.Button.fire(Button.java:185)     at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)     at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)     at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89)     at com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)     at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)     在javafx.event.Event.fireEvent(Event.java:204)     在javafx.scene.Scene $ MouseHandler.process(Scene.java:3746)     在javafx.scene.Scene $ MouseHandler.access $ 1800(Scene.java:3471)     在javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)     在javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2486)     at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:314)     at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:243)     at java.security.AccessController.doPrivileged(Native Method)     at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)     在com.sun.glass.ui.View.handleMouseEvent(View.java:526)     在com.sun.glass.ui.View.notifyMouse(View.java:898)     at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)     at com.sun.glass.ui.gtk.GtkApplication.access $ 200(GtkApplication.java:48)     在com.sun.glass.ui.gtk.GtkApplication $ 6 $ 1.run(GtkApplication.java:149)     在java.lang.Thread.run(Thread.java:745) 引起:java.lang.IllegalArgumentException:找不到无效的URL或资源     在javafx.scene.image.Image.validateUrl(Image.java:1086)     ......还有50个

1 个答案:

答案 0 :(得分:0)

好的,我想我有答案,但我没有测试过,所以我不确定。

但我相信你的问题是编码不一致。

这是您在开始时用来填充gPane的行:

gPane.add(new ImageView("image/card/" + aCardDeck[k++] + ".png"),j,i);

以下是随机化后使用的行:

gPane.add(new ImageView("~/../public/image/card/" + aCardDeck[n++]
        + ".png"),c,r);

我认为您遇到的问题是public/image/card是错误的路径,您需要更改它以使其成为您拥有的另一条路径。另外,我会将它设为refresh方法,而不是在任何地方复制该行代码。因此,每次更改数组时都会调用refresh方法,而不是在任何地方复制+粘贴gPane.add(...)