将加载的内容显示到TextArea中

问题描述 投票:0回答:1

我想将从2种方法加载的信息显示在我的TextArea上。我试图调用和显示的方法名为loadFleetloadCrew,它们位于Fleet类中。我只是把它打印到控制台的地方,就像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();
        }
}
}

这是我的舰队课程:

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;
}
}

请告诉我您希望我提供的其他课程或方法。

java javafx scenebuilder
1个回答
0
投票
  • 通过附加列表的foreach循环将数据添加到shipInfo(您的TextArea)。

快速示例代码:

//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....());
        }
    }

注意:只允许将String值附加到TextArea!

希望能帮助到你!

© www.soinside.com 2019 - 2024. All rights reserved.