如何使用多个场景更新JavaFX Gui中的标签并保持MVC活着

时间:2017-05-08 17:01:04

标签: java javafx

嘿所以这是我的第一个问题,希望你能理解我的问题:

我必须开发一个使用Java和JavaFX的游戏,并尝试从FXML创建的Gui更新标签。我尝试将标签从gui绑定到我的模型中的属性,但如果我调试代码我得到错误:

Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:575)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:204)
at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(LabelSkin.java:49)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
at javafx.beans.property.StringPropertyBase.access$000(StringPropertyBase.java:49)
at javafx.beans.property.StringPropertyBase$Listener.invalidated(StringPropertyBase.java:230)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.binding.StringBinding.invalidate(StringBinding.java:171)
at com.sun.javafx.binding.BindingHelperObserver.invalidated(BindingHelperObserver.java:51)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.IntegerPropertyBase.fireValueChangedEvent(IntegerPropertyBase.java:106)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at model.RegionModel.calculateCurrentEmission(RegionModel.java:199)
at model.RegionModel.run(RegionModel.java:46)

我尝试理解模型 - 视图 - 控制模型,此刻我的应用程序在一个带有一个按钮“开始游戏”的小窗口上启动。如果单击此按钮,则场景将更改为游戏。 这是应用程序的一切开始:

public class CeeOhh22Hell extends Application {

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

//Needed method because it's already a JavaFX-Project
@Override
public void start(Stage primaryStage) throws Exception {
    Hauptmenue start = new Hauptmenue(primaryStage);
    primaryStage.show();
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

这是第一个场景:

public class mainmenu {
Stage primaryStage;
Button btn;
FXMLLoader loader;

public mainmenu(Stage primaryStage){
    this.primaryStage = primaryStage;
    btn = new Button("Start Game");
    Group root = new Group(btn);
    primaryStage.setScene(new Scene(root, 300,300));


    btn.setOnAction(e -> {
        try {
            loader = new FXMLLoader(this.getClass().getResource("Region.fxml"));
            Parent main = loader.load();
            primaryStage.setScene(new Scene(main,1500,800));
            Region_Controller contr = loader.getController();

        } catch (IOException ex) {
            System.out.println("Error");
        }
    });
}

控制器:

public class Region_Controller implements Initializable {


RegionModel region; 

@FXML
private ComboBox<?> settingsComBox;
@FXML
private Label playerName;
@FXML
private Label currentemission;
@FXML
private Label time;
@FXML
private Region gameRegion;    
@FXML
private Button createCow;

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    region = new RegionModel();
    region.start();
    playerName.setText("John Doe");
    bindPropertys();
    createCow.setOnAction(e -> {
        addGameObjectToList(new Cow("Cow",generateFreeCoordinate()));
    });

}

请在开始时记住我刚刚理解JavaFX和MVC。 抱歉我的英语。 感谢

0 个答案:

没有答案