I am currently writing a poker game as an assignment for my finals. I have to use a combined view: a class with actual code and the Scenebuilder. I did everything i learned and the model is working just fine. I'm new to this so it must be an easy fix! The professors want us to get familiar with Scenebuilder but they didn't give much of an explanation.
As you can see i use a class called pokerView in which I load in every single image of the cards. In the beginning I use a strange forloop to automatically generate the names of the images, I printed it out and the url is okay!
I also use the scenebuilder. It has it's own controller/view class that is the FXMLpokerController
And to combine those two, I used a "super" view.
Funny thing is that java does not give me any error!
I really really need an answer. If the window show up empty I cannot show the app.
So the terminal gives me the following lines when using the 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.(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
I don't know what this means
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 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()
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();
}
}
this is the View controller
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();
}
}
}
this is my main 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);
}
}
this is my FXML
解决方案
You have named your AnchorPane "deal" in the FXML and your Button as "deal" in the controller. During initialization, the mapping is done by name. Since these objects are not the same type, you are getting a class exception.
If the button is really deal, move the id from the AnchorPane to the button in the FXML. Based on this:
deal.setOnAction(this::deal);
I assume the button should be called deal.
Change this line
and this line