将加载的内容显示到 TextArea

Display loaded content into TextArea

我想显示我从 2 种方法加载到我的 TextArea 上的信息。我尝试调用和显示的方法名为 loadFleetloadCrew,它们位于 Fleet class 中。我只让它工作到它打印到控制台的地方,就像这样 System.out.println(Enterprise)。我是 Java 和 JavaFX 的新手,所以非常感谢任何帮助。

这是我的 MainController 文件:

package application.controller;
import java.io.IOException;
import application.model.Fleet; 
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class MainController implements EventHandler<ActionEvent>{


@FXML TextField starshipname;
@FXML TextArea shipInfo;
@FXML Button shipEnter;

@Override
public void handle( ActionEvent event) {

    /*try {
        Fleet.loadFleet(null);
    } catch (IOException e) {
        shipInfo.setText(" Starship name not found! ");
        e.printStackTrace();
    }*/

    Fleet Enterprise = new Fleet( "Bozeman" );
     try {
         Enterprise.loadFleet("data/fleet");
         Enterprise.loadCrew("data/personnel");

         System.out.println(Enterprise);
     }catch( IOException e ) {
            System.out.println( "Error loading the file - please check its location." );
            e.printStackTrace();
        }
}
}

这是我的舰队 class:

package application.model;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Fleet {


private String fleetName;
public ArrayList<Starship> ship;
private String fileName;

public Fleet( String name ) {
    this.fleetName = name;
    this.ship = new ArrayList<Starship>();
}

public String getFleetName() {
    return this.fleetName;
}
public void setFleetName( String name ) {
    this.fleetName = name;
}

public ArrayList<Starship> getStarship() {
    return this.ship;
}
public void setStarship( ArrayList<Starship> ship ) {
    this.ship = ship;
}

public String getFileName() {
    return this.fileName;
}
public void setFileName( String fName ) {
    this.fileName = fName;
}

public void addShip(Starship starAdd) {
    this.ship.add(starAdd);
}

public void getStarshipsByName( Starship starName ) {
    Starship starname;
}

public void loadFleet(String fileName)throws IOException {
    try {
        Scanner scan = new Scanner(new File("data/fleet.csv") );

        while( scan.hasNextLine() ) {
            String line = scan.nextLine();
            String[] items = line.split(",");
            Starship tmp = null;

            tmp = new Starship( items[0], items[1], items[2]);
            this.addShip(tmp);
        }
        scan.close();
    }catch(IOException e) {
        e.printStackTrace();
    }
}
public void loadCrew(String fileName)throws IOException {
    try {
    Scanner scan = new Scanner(new File("data/personnel.csv") );

    while( scan.hasNextLine() ) {
        String line = scan.nextLine();
        String[] items = line.split(",");
        Crewmember tmp = null;

        tmp = new Crewmember( items[0], items[1], items[2], items[3], items[4]);

        for(int i = 0; i < this.ship.size(); ++i) {
            if(this.ship.get(i).getStarShipRegistry().equals(items[3]))
                    this.ship.get(i).addCrew( tmp );
        }
    }
    scan.close();
} catch(IOException e) {
    e.printStackTrace();
}
}
public String toString() {
    String ret = " ";

    for( int i = 0; i < this.ship.size(); i++)
    {
        ret += this.ship.get(i).toString();
    }
    return ret;
}
}

请让我知道您还希望我提供哪些其他 classes 或方法。

  • 通过从 foreach 追加数据到 shipInfo(您的 TextArea) list 的循环。

快速示例代码:

//instance of your ship object
StarShip myShip = StarShip();
//method to add data to your TextArea
private void showShipInfo(){
    //getStarShip returns the list of ships
    //same as the method on your Fleet class
    myShip.getStarShip().forEach(starShip ->{
        //add data to TextArea for each field of
        //StarShip object you want to display
        shipInfo.append(starShip.getName());
        //shipInfo.append(starShip.get....());
        //shipInfo.append(starShip.get....());
        }
    }

Note: only String values are allowed to be appended to TextArea!

希望对您有所帮助!