将加载的内容显示到TextArea中

时间:2019-02-28 01:46:20

标签: java javafx scenebuilder

我想显示从2种方法加载到我的TextArea上的信息。我尝试调用和显示的方法分别名为loadFleet类中的loadCrewFleet。我只能像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;
}
}

请让我知道您还想提供什么其他类或方法。

1 个答案:

答案 0 :(得分: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....());
        }
    }
  

注意:只能将字符串值附加到TextArea!

希望有帮助!