我正在使用Javafx
格式进行Model View Controller
项目。目前在模型中,我有3个班级:学校班级,专业班级和学生班级。学校班级有一系列的专业,而专业班级有一系列的学生。从majorName.txt
个文件中读取了学生列表。
该视图将专业和学生的列表组织并打印到文本字段中。所需的方法之一是能够在按下保存按钮后保存对文本字段所做的任何更改(因为使用该应用程序的人可以更改名称,添加名称,删除名称等)。 Controller不应保存和更新文件。相反,应该从学校课堂上调用一种方法来进行保存。
main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// Load the FXML document (we created with SceneBuilder)
FXMLLoader loader = new FXMLLoader();
loader.setLocation( Main.class.getResource("../Main.fxml") );
// Load the layout from the FXML and add it to the scene
AnchorPane layout = (AnchorPane) loader.load();
Scene scene = new Scene( layout );
// Set the scene to stage and show the stage to the user
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
School.Java
public class School {
private String name;
private ArrayList<Major> majors;
public School(String name) {
this.name = name;
this.Major = new ArrayList<Major>();
}
public School loadData( ) {
//reading .txt files to create instances of majors and students
}
public void Save( String houseName, String roster ) {
//writing .txt files with changes from application textfields
}
maincontroller.java
public class MainController implements EventHandler {
@FXML private Text schoolName;
@FXML private Text firstHouseName;
@FXML private Text firstHouseStudent;
//ect... LIST of all FXML nodes
public void initialize( URL location, ResourceBundle resources) {
//Create the school instance and call School.loadData() reading data to create major and students.
School OCC = new School( "OCC");
OCC.loadData();
public void handle(Event arg0) {
// TODO Auto-generated method stub
String rosterOne = firstMajorStudent.getText();
String nameOne = firstMajorName.getText();
//OCC Cannot be resolved
OCC.save(nameOne, rosterOne);
我的问题是代码的最后一行,从班级学校调用Save方法。我需要将OCC
对象传递给handle方法吗?。