我最近制作了一个java swing应用程序来播放oxo,我正在尝试使用MVC设计模式在javaFX中重新创建它。我对于应用程序的哪一部分应该处理什么感到困惑。
它看起来如何摆动(见图片链接)
javaFX中使用fxml的设计(见图片链接)
文件夹结构是(见图片链接)
为了使这个抽象的问题更具体:我处理控制器中的点击,但是我该怎么做呢?我在那里处理我的游戏逻辑吗?或者我应该确定来源并将其发送到我的主应用程序来完成剩下的工作?
Main:
package be.vincent_nagy.oxo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("oxo.fxml"));
Parent root = loader.load();
Scene mainScene = new Scene(root,600,600);
primaryStage.setTitle("OXO Spel");
primaryStage.setScene(mainScene);
primaryStage.show();
//Hier geef ik de controller toegang tot de main app.
OxoController controller = loader.getController();
controller.setMain(this);
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package be.vincent_nagy.oxo;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
public class OxoController {
@FXML
private GridPane mainGrid;
@FXML
private Button button5;
@FXML
private Button button3;
@FXML
private Button button2;
@FXML
private Button button4;
@FXML
private Button button6;
@FXML
private Button button7;
@FXML
private Button button8;
@FXML
private Button button9;
@FXML
private Button button1;
//Referentie naar de main applicatie
private Main main;
//Constructor wordt opgeroepen voor de initialize() methode
public OxoController(){
}
// tot de @FXML properties van deze controller
@FXML
private void initialize(){
}
//Referentie naar de main applicatie. Aanroeping vanuit main
public void setMain(Main main){
this.main = main;
}
@FXML
void handleButtonAction(ActionEvent event) {
if(event.getSource() instanceof Button){
Button src = (Button) event.getSource();
//send the src up to main or handle the game code here?
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane fx:id="mainGrid" alignment="center" gridLinesVisible="true" minHeight="600.0" minWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="be.vincent_nagy.oxo.OxoController">
<rowConstraints>
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
</rowConstraints>
<columnConstraints>
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
</columnConstraints>
<children>
<Button fx:id="button5" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button fx:id="button3" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" />
<Button fx:id="button2" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" />
<Button fx:id="button4" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" />
<Button fx:id="button6" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Button fx:id="button7" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" />
<Button fx:id="button8" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button fx:id="button9" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Button fx:id="button1" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" />
</children>
</GridPane>