运行javaFX应用程序时出现的空窗口,同时使用图形视图和场景构建器

时间:2015-12-26 18:45:10

标签: java javafx scenebuilder

我目前正在为我的决赛写一个扑克游戏。我必须使用组合视图:具有实际代码的类和Scenebuilder。我做了我学到的一切,模特工作得很好。我是新手,所以一定要轻松解决!教授希望我们熟悉Scenebuilder,但他们没有给出太多解释。

正如你所看到的,我使用了一个名为pokerView的类,我在其中加载了每张卡片的图像。一开始我使用一个奇怪的forloop自动生成图像的名称,我打印出来并且网址没问题!

我也使用了scenebuilder。它拥有自己的控制器/视图类,即FXMLpokerController

为了将这两者结合起来,我使用了一个" super"视图。

有趣的是java不会给我任何错误!

我真的需要一个答案。如果窗口显示为空,我无法显示应用程序。

因此终端在使用printstacktrace

时给出了以下行
    javafx.fxml.LoadException: 
   file:/Users/test/Documents/2%20aba/Informatica/Netbeans%20projecten/poker/dist/run1845271650/poker.jar!/poker/FXMLpoker.fxml:10

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at poker.ViewPlus.<init>(ViewPlus.java:30)
at poker.MAINpoker.start(MAINpoker.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

    Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.Button field poker.FXMLpokerController.deal to javafx.scene.layout.AnchorPane
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 10 more

我不知道这意味着什么

    this is the pokerview 
    package poker;


import javafx.scene.layout.Region;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import java.util.ArrayList;
import java.net.URL;



public class PokerView extends Region{
    private Game model;
    private Image image;
    private ImageView imv;
    private ArrayList<ImageView> stapelView =  new ArrayList<>();


    public PokerView(Game model){
        this.model=model;
    }


    public void configureerStapel(){
    for(Kaart k:model.getRound().getStapel().getList()){
        URL imageURL = getClass().getResource("/res/"+k.toString());
        System.out.println(""+imageURL);
        imv = new ImageView(imageURL.toExternalForm());

        stapelView.add(imv);


    }


}

public void turnHandCards(){
    int index1 = model.getRound().wieIsAanDeBeurt().getHand().get(0).getIndex();
    ImageView k = stapelView.get(index1);
    System.out.println("de eerste hand card in de view ="+index1);
    k.setLayoutX(166);
    k.setLayoutY(200);
    k.setFitHeight(141);
    k.setFitWidth(95);


    int index2 = model.getRound().wieIsAanDeBeurt().getHand().get(1).getIndex();
    ImageView s = stapelView.get(index2);
    s.setLayoutX(297);
    s.setLayoutY(200);
    s.setFitHeight(141);
    s.setFitWidth(95);
    getChildren().addAll(k, s);
}

public void turnThreeTableCards(){

        int index1 = model.getRound().getTafel().getFirstCard().getIndex();
        ImageView i = stapelView.get(index1);
        i.setLayoutX(447);
        i.setLayoutY(24);
        i.setFitHeight(141);
        i.setFitWidth(95);

        int index2 = model.getRound().getTafel().getSecondCard().getIndex();
        ImageView j = stapelView.get(index2);
        j.setLayoutX(340);
        j.setLayoutY(24);
        j.setFitHeight(141);
        j.setFitWidth(95);

        int index3 = model.getRound().getTafel().getThirdCard().getIndex();
        ImageView k = stapelView.get(index3);
        k.setLayoutX(231);
        k.setLayoutY(24);
        k.setFitHeight(141);
        k.setFitWidth(95);

        getChildren().addAll(i, j, k);

}

public void turnLastTwoTableCards(){
    int index1 = model.getRound().getTafel().getFourthCard().getIndex();
        ImageView i = stapelView.get(index1);
        i.setLayoutX(123);
        i.setLayoutY(24);
        i.setFitHeight(141);
        i.setFitWidth(95);

        int index2 = model.getRound().getTafel().getFifthCard().getIndex();
        ImageView j = stapelView.get(index2);
        j.setLayoutX(14);
        j.setLayoutY(24);
        j.setFitHeight(141);
        j.setFitWidth(95);
        getChildren().addAll(i, j);


}

}

    this is the FXMLpokerController
    package poker;

import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import java.lang.*;

public class FXMLpokerController {

@FXML
private Button raise;

@FXML
private Button check;

@FXML
private Button fold;

@FXML
private Button ready;

@FXML
private TextField stake;

@FXML
private TextArea creditsLeft;

@FXML
private TextArea pot;

@FXML
private TextArea minimumStake;

@FXML
private TextArea isAanDeBeurt;

@FXML
private Button deal;



@FXML
void initialize(){
    raise.setOnAction(this::raise);
    check.setOnAction(this::check);
    fold.setOnAction(this::fold);
    ready.setOnAction(this::ready);
    deal.setOnAction(this::deal);
    pot.setText(""+model.getPot());
    creditsLeft.setText(""+model.getRound().wieIsAanDeBeurt().getCredit());

}

private Game model;
private ViewPlus view;
private int counter;



public void raise(ActionEvent e){
    int cred = Integer.parseInt(stake.getText());
    model.getRound().wieIsAanDeBeurt().setStake(cred);
    model.getRound().wieIsAanDeBeurt().setCredit(model.getRound().wieIsAanDeBeurt().getCredit()-cred);
    model.getRound().getTafel().setPot(model.getRound().getTafel().getPot()+cred);
    model.getRound().volgendeSpeler();
    minimumStake.setText(""+cred);
    automaticFold();
}

public void check(ActionEvent e){
    model.getRound().volgendeSpeler();
    automaticFold();

}

public void fold(ActionEvent e){
    model.getRound().wieIsAanDeBeurt().doetNietMeerMee(true);
    automaticFold();

}

public void setModel(Game model){
    this.model=model;
    model = new Game();
}

public void setView(ViewPlus view){
    this.view=view;
}

public void automaticFold(){
    int cred = Integer.parseInt(minimumStake.getText());
    if(model.getRound().wieIsAanDeBeurt().getCredit()<cred){
        model.getRound().wieIsAanDeBeurt().doetNietMeerMee(true);
    }
}

public void ready(ActionEvent e){
    model.getRound().volgendeSpeler();
}

public void setName(){
    isAanDeBeurt.setText(model.getRound().wieIsAanDeBeurt().getName());
    String name = isAanDeBeurt.getText();
    model.getRound().wieIsAanDeBeurt().setName(name);

}

public void deal(ActionEvent e){
    model.startOver();
    view.configureerKaarten();
    view.turnHandCards();
    counter++;
}

/**
 * @return the counter
 */
public int getCounter() {
    return counter;
}

}

    this is the superview called viewplus

package poker;

import javafx.scene.Parent;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;

public class ViewPlus extends AnchorPane {
private Game model;
private PokerView view;
private Parent fxmlView;
private FXMLpokerController fxmlController;

public ViewPlus(Game model) throws IOException{
    this.model = model;
    view = new PokerView(model);
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("FXMLpoker.fxml"));
    try{
        fxmlView = loader.load();
        fxmlController = loader.getController();
        fxmlController.setModel(model);
        fxmlController.setView(this);
        getChildren().addAll(fxmlView,view);  
    } catch(IOException ex){ex.printStackTrace();}

}

public void configureerKaarten(){
    view.configureerStapel();
}

public void turnHandCards(){
    view.turnHandCards();
}

public void turnThreeTableCards(){
    view.turnThreeTableCards();
}

}

这是View控制器

    package poker;


    public class Controller {

private Game model;
private ViewPlus view;

public Controller(Game model, ViewPlus view){
    this.model=model;
    this.view=view;
    view.configureerKaarten();
    view.turnHandCards();
    if(model.getRound().getSpeler1().getStake() == model.getRound().getSpeler2().getStake()
    && model.getRound().getSpeler2().getStake() == model.getRound().getSpeler3().getStake()
    && model.getRound().getSpeler3().getStake() == model.getRound().getSpeler4().getStake()){
        view.turnThreeTableCards();
    }


}

}

这是我的主要课程

   package poker;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class MAINpoker extends Application {

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

    Game model = new Game();



    ViewPlus view = new ViewPlus (model);


    Scene scene = new Scene(view);

    stage.setScene(scene);
    stage.show();

    Controller c = new Controller(model, view); 

}

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

}

这是我的FXML

    <?xml version="1.0" encoding="UTF-8"?>

    <?import java.lang.*?>
    <?import javafx.scene.image.*?>
    <?import java.util.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>

    <AnchorPane id="AnchorPane" fx:id="deal" prefHeight="605.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="poker.FXMLpokerController">
<children>
  <Button layoutX="484.0" layoutY="556.0" mnemonicParsing="false" text="ready" />
  <Button layoutX="249.0" layoutY="495.0" mnemonicParsing="false" text="deal" />
  <Label layoutX="393.0" layoutY="495.0" prefHeight="42.0" prefWidth="160.0" text="minimum stake">
     <graphic>
        <TextArea fx:id="minimumStake" prefHeight="28.0" prefWidth="28.0" />
     </graphic>
  </Label>
    <Button fx:id="raise" layoutX="14.0" layoutY="405.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="69.0" text="raise" />
  <Button fx:id="check" layoutX="251.0" layoutY="405.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="69.0" text="check" />
  <Button fx:id="fold" layoutX="473.0" layoutY="405.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="69.0" text="fold" />
  <Label layoutX="17.0" layoutY="548.0" prefHeight="42.0" prefWidth="148.0" text="name">
     <graphic>
        <TextArea fx:id="isAanDeBeurt" prefHeight="42.0" prefWidth="97.0" />
     </graphic>
  </Label>
  <Label layoutX="17.0" layoutY="495.0" text="pot">
     <graphic>
        <TextArea fx:id="pot" prefHeight="36.0" prefWidth="47.0" />
     </graphic>
  </Label>
    <Label fx:id="label" layoutX="409.0" layoutY="450.0" minHeight="16" minWidth="69" text="stake">
     <graphic>
        <TextField prefHeight="20.0" prefWidth="91.0" />
     </graphic></Label>
  <ImageView fx:id="tafel" fitHeight="391.0" fitWidth="556.0">
     <image>
        <Image url="@../afbeelding/dark_green_colour_background.jpg" />
     </image>
  </ImageView>
  <Pane layoutX="14.0" layoutY="24.0">
     <children>
        <ImageView fx:id="tafel1" fitHeight="141.0" fitWidth="95.0">
           <image>
              <Image url="@../afbeelding/back.jpg" />
           </image>
        </ImageView>
     </children>
  </Pane>
  <Label fx:id="label1" layoutX="17.0" layoutY="450.0" minHeight="16" minWidth="69" prefHeight="16.0" prefWidth="119.0" text="credits left">
     <graphic>
        <TextArea fx:id="creditsLeft" prefHeight="16.0" prefWidth="37.0" />
     </graphic>
  </Label>
  <Pane layoutX="142.0" layoutY="246.0" />
  <Pane layoutX="123.0" layoutY="24.0">
     <children>
        <ImageView fx:id="tafel2" fitHeight="141.0" fitWidth="95.0">
           <image>
              <Image url="@../afbeelding/back.jpg" />
           </image>
        </ImageView>
     </children>
  </Pane>
  <Pane layoutX="231.0" layoutY="24.0">
     <children>
        <ImageView fx:id="tafel3" fitHeight="141.0" fitWidth="95.0">
           <image>
              <Image url="@../afbeelding/back.jpg" />
           </image>
        </ImageView>
     </children>
  </Pane>
  <Pane layoutX="340.0" layoutY="24.0">
     <children>
        <ImageView fx:id="tafel4" fitHeight="141.0" fitWidth="95.0">
           <image>
              <Image url="@../afbeelding/back.jpg" />
           </image>
        </ImageView>
     </children>
  </Pane>
  <Pane layoutX="447.0" layoutY="24.0">
     <children>
        <ImageView fx:id="tafel5" fitHeight="141.0" fitWidth="95.0">
           <image>
              <Image url="@../afbeelding/back.jpg" />
           </image>
        </ImageView>
     </children>
  </Pane>
  <Pane layoutX="166.0" layoutY="200.0">
     <children>
        <ImageView fx:id="hand1" fitHeight="141.0" fitWidth="95.0">
           <image>
              <Image url="@../afbeelding/back.jpg" />
           </image>
        </ImageView>
     </children>
  </Pane>
  <Pane layoutX="297.0" layoutY="200.0">
     <children>
        <ImageView fx:id="hand2" fitHeight="141.0" fitWidth="95.0">
           <image>
              <Image url="@../afbeelding/back.jpg" />
           </image>
        </ImageView>
     </children>
  </Pane>
</children>

2 个答案:

答案 0 :(得分:0)

你的AnchorPane有fx:id“deal”:

<AnchorPane id="AnchorPane" fx:id="deal" prefHeight="605.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="poker.FXMLpokerController">

在你的控制器类FXMLpokerController中,你将“deal”定义为一个按钮:

@FXML
private Button deal;

FXML注入无法将Button初始化为AnchorPane。将fx:id移动到正确的按钮,并为AnchorPane提供不同的fx:id。

答案 1 :(得分:0)

您已在FXML中将您的AnchorPane命名为“deal”,并将您的Button命名为控制器中的“deal”。在初始化期间,映射按名称完成。由于这些对象的类型不同,因此您将获得类异常。

如果按钮真的处理完毕,请将id从AnchorPane移动到FXML中的按钮。基于此:

deal.setOnAction(this::deal);

我认为该按钮应该被称为交易。

更改此行

    <AnchorPane id="AnchorPane" prefHeight="605.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="poker.FXMLpokerController">

和这一行

<Button fx:id="deal" layoutX="249.0" layoutY="495.0" mnemonicParsing="false" text="deal" />