运行 javaFX 应用程序同时使用图形视图和场景生成器时出现空 window

Empty window showing up when running javaFX application, using both graphic view and scenebuilder

我目前正在编写一个扑克游戏作为期末考试的作业。我必须使用组合视图:带有实际代码和 Scenebuilder 的 class。我做了我学到的一切,模型工作得很好。我是新手,所以一定很容易解决!教授们希望我们熟悉 Scenebuilder,但他们没有给出太多解释。

如您所见,我使用了一个名为 pokerView 的 class,我在其中加载了纸牌的每张图像。一开始我用了一个奇怪的forloop来自动生成图片的名字,打印出来url就可以了!

我也用场景生成器。它有自己的 controller/view class 即 FXMLpokerController

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

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

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

所以终端在使用 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$launchApplication13(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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.access00(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();
}

}

这是视图控制器

    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();
    }


}

}

这是我的主class

   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>

您的 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">

在您的控制器 class FXMLpokerController 中,您将 "deal" 定义为按钮:

@FXML
private Button deal;

FXML 注入无法将 Button 初始化为 AnchorPane。将 fx:id 移动到正确的按钮并给你的 AnchorPane 一个不同的 fx:id.

您已在 FXML 中将 AnchorPane 命名为 "deal",在控制器中将按钮命名为 "deal"。在初始化期间,映射是按名称完成的。由于这些对象不是同一类型,您会收到 class 异常。

如果按钮真的很重要,将 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" />