图像不显示Javafx

时间:2015-04-26 20:05:07

标签: java image netbeans javafx

package DiceProject;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.layout.Pane;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javax.swing.*;
import java.lang.Object;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.layout.Region;
import javafx.scene.control.Control;
import javafx.scene.control.Labeled;
import javafx.scene.control.Label;
/**
 *
 * @author Ethan C.B. Powell
 */
public class DiceProject extends Application {
double Bankroll;
double Loses;
double Winnings;

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();

        String BetAmountString;

        double BetAmount;
        Bankroll account1 = new Bankroll();

        BetAmountString = JOptionPane.showInputDialog(null, "Enter your wager: ");
        BetAmount = Integer.parseInt(BetAmountString);

        Button btn = new Button();
        btn.setText("Ready to Play???");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

            //NEEDS MUSIC


            int Dice1 = 1 + (int)(Math.random()*6);

            switch (Dice1) {
            case 1:
              Image diceface11 = new Image("/DiceProject/diceface1.png", true);
              ImageView diceresult11 = new ImageView(diceface11);
              diceresult11.setX(200);
              diceresult11.setY(200);
                     break;
            case 2:
             Image diceface12 = new Image("/DiceProject/diceface2.png", true);
              ImageView diceresult12 = new ImageView(diceface12);
              diceresult12.setX(200);
              diceresult12.setY(200);
                     break;
            case 3:
             Image diceface13 = new Image("/DiceProject/diceface3.png", true);
              ImageView diceresult13 = new ImageView(diceface13);
              diceresult13.setX(200);
              diceresult13.setY(200);
                     break;
            case 4:
             Image diceface14 = new Image("/DiceProject/diceface4.png", true);
              ImageView diceresult14 = new ImageView(diceface14);
              diceresult14.setX(200);
              diceresult14.setY(200);
                     break;
            case 5:
             Image diceface15 = new Image("/DiceProject/diceface5.png", true);
              ImageView diceresult15 = new ImageView(diceface15);
              diceresult15.setX(200);
              diceresult15.setY(200);
                     break;
            case 6:
             Image diceface16 = new Image("/DiceProject/diceface6.png", true);
              ImageView diceresult16 = new ImageView(diceface16);
              diceresult16.setX(200);
              diceresult16.setY(200);
                     break;
            default:
              Image diceface17 = new Image("/DiceProject/diceface1.png", true);
              ImageView diceresult17 = new ImageView(diceface17);
              diceresult17.setX(200);
              diceresult17.setY(200);
                     break;
            }

      int Dice2 = 1 + (int)(Math.random()*6);
      switch (Dice2) {
            case 1:
              Image diceface21 = new Image("/DiceProject/diceface1.png", true);
              ImageView diceresult21 = new ImageView(diceface21);
              diceresult21.setX(400);
              diceresult21.setY(200);
                     break;
            case 2:
             Image diceface22 = new Image("/DiceProject/diceface2.png", true);
              ImageView diceresult22 = new ImageView(diceface22);
              diceresult22.setX(400);
              diceresult22.setY(200);
                     break;
            case 3:
             Image diceface23 = new Image("/DiceProject/diceface3.png", true);
              ImageView diceresult23 = new ImageView(diceface23);
              diceresult23.setX(400);
              diceresult23.setY(200);
                     break;
            case 4:
             Image diceface24 = new Image("/DiceProject/diceface4.png", true);
              ImageView diceresult24 = new ImageView(diceface24);
              diceresult24.setX(400);
              diceresult24.setY(200);
                     break;
            case 5:
             Image diceface25 = new Image("/DiceProject/diceface5.png", true);
              ImageView diceresult25 = new ImageView(diceface25);
              diceresult25.setX(400);
              diceresult25.setY(200);
                     break;
            case 6:
             Image diceface26 = new Image("/DiceProject/diceface6.png", true);
              ImageView diceresult26 = new ImageView(diceface26);
              diceresult26.setX(400);
              diceresult26.setY(200);
                     break;
            default:
                     Image diceface27 = new Image("/DiceProject/diceface1.png", true);
              ImageView diceresult27 = new ImageView(diceface27);
              diceresult27.setX(400);
              diceresult27.setY(200);
                     break;
            }


                if((Dice1 > Dice2)){ //if you win
                    Winnings = BetAmount;
                    account1.SetWinBalance(Winnings);
            }
                else if (Dice1 < Dice2) { //you lose
                    Loses = BetAmount;
                    account1.SetLoseBalance(Loses);
                    account1.GetBalance();
                    if (Bankroll <= 0) { //INSERT BANKRUPT.PNG HERE
                    }
                        }
                else if (Dice1 == Dice2) { //tie
              Image ACD = new Image("/DiceProject/RollAgain.png", true);
              ImageView Reroll = new ImageView(ACD); //for some reason, a problem comes up whe i use "Reroll" as the name
              Reroll.setX(400);
              Reroll.setY(200);
                }
                else {
                //How did you even do this?
                }
            }
        });


        StackPane root = new StackPane();
        root.getChildren().add(btn);


        Scene scene = new Scene(root, 500, 500);

        primaryStage.setTitle("Play Dice");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public class Bankroll {
        private double Bankroll;

        Bankroll() {
        Bankroll = 200;
        }
        public double GetBalance(){
        return Bankroll;
        }
        public void SetLoseBalance(double Loses){
        Bankroll = Bankroll - Loses;
        }
        public void SetWinBalance(double Winnings) {
        Bankroll = Bankroll + Winnings;}
    }

    /**
     * @param args the command line arguments
     */


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

}

它应该显示一个与骰子滚动相对应的图像,但没有任何反应。 有问题的图像在包中(如果重要的话,单击并拖动到项目窗口中)。我在System.out.print中添加了t显示Dice1和2的结果,所以我知道这有效。

我是否需要添加其他内容才能使其显示,我之前从未使用过JavaFX中的实际图像....一般来说,它是JavaFX的新手。

GetChildren的错误消息 C:\ Users \ Ethan C.B. Powell \ Documents \ 15562 \ DICE PROjECT \ src \ DiceProject \ DiceProject.java:220:错误:类,接口或枚举预期     public static void main(String [] args){

C:\ Users \ Ethan C.B. Powell \ Documents \ 15562 \ DICE PROJECT \ src \ DiceProject \ DiceProject.java:222:错误:类,接口或枚举预期     } 2个错误

C:\ Users \ Ethan C.B. Powell \ Documents \ 15562 \ DICE PROJECT \ nbproject \ build-impl.xml:924:执行此行时发生以下错误:

C:\ Users \ Ethan C.B. Powell \ Documents \ 15562 \ DICEPROJECT \ nbproject \ build-impl.xml:264:编译失败;请参阅编译器错误输出以获取详细信 BUILD FAILED(总时间:0秒)

3 个答案:

答案 0 :(得分:0)

您永远不会向ImageView个孩子添加任何StackPane's个组件。您只需添加一个ImageView并根据EventHandler中返回的骰子编号更改其图像。

答案 1 :(得分:0)

扩展上一个答案,您已经拥有了要添加要创建的图形的按钮。您必须创建图形,然后将其添加到Button。例如:

Image diceface11 = new Image("/DiceProject/diceface1.png", true);
ImageView diceresult11 = new ImageView(diceface11);
// Sets diceresult11 to the graphic on btn
btn.setGraphic(diceresult11);

顺便说一句,你也可以减少一些重复的行

// Initialize variable for the image location
String imageLocation = "";
switch(Dice 1){
    // Each case just sets the image location
    case 1: imageLocation = "/DiceProject/diceface1.png";
    case 2: imageLocation = "/DiceProject/diceface2.png";
    // And so on...
}
// The code that was in each case can be put once, after the switch
// Because imageLocation will be set, as long as it is set in each case
// And there is a default case
Image diceFace = new Image(imageLocation, true);
ImageView diceResult = new ImageView(diceFace);
diceresult17.setX(200);
diceresult17.setY(200);
btn.setGraphic(diceResult);

答案 2 :(得分:0)

 case 1:
          Image diceface11 = new Image("/DiceProject/diceface1.png", true);
          ImageView diceresult11 = new ImageView(diceface11);
          diceresult11.setX(200);
          diceresult11.setY(200);
                 break;

这将创建ImageView的一个实例。

在此之后,将不再使用该实例。