如何使用 JavaFX GUI 应用程序显示 .txt 文件中的数据
How to display data from .txt file using JavaFX GUI Application
我想使用 JavaFX 显示来自 .txt 报告文件的数据。在我的代码中,我尝试使用 Labels 和 Vbox 在 GUI 场景中以多种格式显示信息。但是,我在将结果输出为 GUI 而不是控制台时遇到了很糟糕的情况。我试图研究我的问题,但找不到解决问题所需的信息。
这是我需要使用 JavaFX 显示为 GUI 应用程序的报告:
这是我的代码显示为 GUI 的内容:
这是我的源代码:
package sample;
public class CustomerSale {
// Details of reports
private String zipCodeExtension;
private int customerNumber;
private String customerName;
private int purchaseDate;
private String make;
private int purchasePrice;
private int yearOfVehicle;
private int satisfactionRating;
// Create constructor argument
public CustomerSale(String zipCodeExtension, int customerNumber, String customerName,
int purchaseDate, String make, int purchasePrice,
int yearOfVehicle, int satisfactionRating) {
this.zipCodeExtension = zipCodeExtension;
this.customerNumber = customerNumber;
this.customerName = customerName;
this.purchaseDate = purchaseDate;
this.make = make;
this.purchasePrice = purchasePrice;
this.yearOfVehicle = yearOfVehicle;
this.satisfactionRating = satisfactionRating;
}
// Create getters and setters
public String getMake() {
return make;
}
public String getZipCodeExtension() {
return zipCodeExtension;
}
public int getCustomerNumber() {
return customerNumber;
}
public String getCustomerName() {
return customerName;
}
public int getPurchaseDate() {
return purchaseDate;
}
public double getPurchasePrice() {
return purchasePrice;
}
public int getYearOfVehicle() {
return yearOfVehicle;
}
public int getSatisfactionRating() {
return satisfactionRating;
}
}
package sample;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.File;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
/**
ABC used cars report Application
*/
public class FinalGUIOutput extends Application {
final int WINDOW_WIDTH = 750, // Window width in pixels
WINDOW_HEIGHT = 150; // Window height in pixels
public static void main(String[] args) {
// Launch the application
launch(args);
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
// Create a Label to display the heading and page number.
Label headingLabel = new Label("---------------------------------------------------------ABC USED CARS--------------------------------------------------------");
Label pageNumber = new Label(
"-------------------------------------------------------PAGE 1-----------------------------------------------------------------");
// Label pageFormat = new Label("%-19s%-15s%-21s%-15s%-20s%-17s%-12s%-20s\n", "|ZIP CODE-EXT|", "|CUST No.|", "|CUSTOMER NAME|", "|DoP|",
// "|MAKE|", "|PRICE|", "|YEAR|", "|RATE|");
Label pageLine = new Label(
"------------------------------------------------------------------------------------------------------------------------------");
// Create an empty Label to display the result.
Label resultLabel = new Label();
File carsInfo = new File("custsale.txt");
Scanner carsScanner = new Scanner(carsInfo);
String format = "%-20s%-14s%-21s%-16s%-19s$%-17s%-14s%-20s\n";
final int pageSize = 30;
int carCounter = 0;
while (carsScanner.hasNextLine()) {
if (carCounter % pageSize == 0) {
printPageHeader(carCounter / pageSize + 1);
}
// Read the carsScanner
String zipCodeExtension = carsScanner.nextLine();
int customerNumber = Integer.parseInt(carsScanner.nextLine());
String customerName = carsScanner.nextLine();
int purchaseDate = Integer.parseInt(carsScanner.nextLine());
String make = carsScanner.nextLine();
int purchasePrice = Integer.parseInt(carsScanner.nextLine());
int yearOfVehicle = Integer.parseInt(carsScanner.nextLine());
int satisfactionRating = Integer.parseInt(carsScanner.nextLine());
CustomerSale custsale = new CustomerSale(zipCodeExtension, customerNumber, customerName,
purchaseDate, make, purchasePrice, yearOfVehicle, satisfactionRating);
// Display the report
DecimalFormat df = new DecimalFormat("#,#####");
System.out.printf(format, custsale.getZipCodeExtension(), custsale.getCustomerNumber(),
custsale.getCustomerName(), custsale.getPurchaseDate(), custsale.getMake(),
df.format(custsale.getPurchasePrice()), custsale.getYearOfVehicle(),
custsale.getSatisfactionRating());
// Increment the car counter
carCounter++;
}
carsScanner.close();
/**
GUI Display
*/
// Put the HBox, calcButton and resultLabel in a VBox.
VBox vbox = new VBox(10,headingLabel, pageNumber, pageLine, resultLabel);
// Set the VBox's alignment to center.
vbox.setAlignment(Pos.CENTER);
// Set the VBox's padding to 10 pixels
vbox.setPadding(new Insets(10));
// Create a Scene with the VBox as its root node.
Scene scene = new Scene(vbox, WINDOW_WIDTH, WINDOW_HEIGHT);
// set the scene's alignment to center.
vbox.setAlignment(Pos.CENTER);
//Add the Scene to the Stage
primaryStage.setScene(scene);
// Set the stage title
primaryStage.setTitle("ABC Used Cars");
// Show the window
primaryStage.show();
}
private static void printPageHeader(int i) {
// Create a Label to display the heading and page number.
Label headingLabel = new Label("---------------------------------------------------------ABC USED CARS--------------------------------------------------------");
Label pageNumber = new Label(
"-------------------------------------------------------" + "PAGE " + i + "-----------------------------------------------------------------");
// Label pageFormat = new Label("%-19s%-15s%-21s%-15s%-20s%-17s%-12s%-20s\n", "|ZIP CODE-EXT|", "|CUST No.|", "|CUSTOMER NAME|", "|DoP|",
// "|MAKE|", "|PRICE|", "|YEAR|", "|RATE|");
Label pageLine = new Label(
"------------------------------------------------------------------------------------------------------------------------------");
}
}
最后这是我试图从中提取数据的 .txt 文件报告:
46410-1234
1001
ALBERT, CARL T.
08252001
FORD
0255000
1991
2
46307-1201
1003
ANDREWS, ROBERT
08262001
CHEVROLET
0700000
1958
0
46423-2311
1008
ANZIO, RAFELINO
09012001
CHEVROLET
0456050
1978
0
46424-0121
1010
ASHLEY, WILLIAM B.
09022001
PIERCE-ARROW
1240000
1932
2
46375-3110
1015
ATKINSON, MARK
08272001
STUDEBAKER
0050000
1958
1
46405-1291
1025
AVERY, ALFRED A.
08292001
HUDSON
0230000
1954
1
46301-1234
1031
BEZZMEK, JENNIFER
08272001
FORD
0455000
1995
2
46303-1201
1033
BLAKE, DONALD
08292001
FORD
0722050
1989
1
46413-2311
1041
BLONDELL, BONNIE
09042001
CHEVROLET
0356050
1988
1
46404-0121
1045
BONADIO, JAMES
09032001
PIERCE-ARROW
1150000
1935
2
46307-3110
1055
BUCKO, ONIEDA
08282001
STUDEBAKER
0245000
1964
1
46410-1291
1056
BYMANN, FREDRICK
08292001
FORD
0330000
1994
1
46342-1234
1061
CALBERT, RONALD
09052001
FORD
0355000
1993
2
46307-1211
1063
CHELSEA, MARTHA S.
08302001
DODGE BROS.
0350000
1935
0
46410-2311
1067
CLAFLIN, WAYNE R.
09012001
CHEVROLET
0456050
1990
2
46410-0121
1070
COLE, CHARLES C.
09022001
PIERCE-ARROW
1050000
1929
2
46305-3111
1075
COLEMAN, THOMAS
08272001
STUDEBAKER
0167050
1961
1
46410-1221
1076
COLWELL, RICHARD L.
08292001
HUDSON
0430000
1940
1
46414-1231
1080
COOPER, JOHNATHAN
09052001
FORD
1256000
1996
2
46307-1201
2002
COREY, SARAH D.
08272001
CHEVROLET
0650050
1994
0
46421-2311
2004
CRACKLIN, GOODMAN
09012001
CHEVROLET
1456050
1996
0
46323-0121
2011
CRAWFORD, TIMOTHY
09012001
PIERCE-ARROW
1040000
1931
2
46315-3110
2012
CURRIE, RAYMOND
08282001
STUDEBAKER
0150025
1956
1
46425-1291
2024
CYBORG, IZORE M.
09052001
HUDSON
0130000
1949
1
46410-1234
2031
DALTON, DAVID P.
08252001
FORD
0234000
1990
2
46307-1201
2043
DAVIES, RALPH O.
08262001
CHEVROLET
0333000
1989
0
46423-2311
2048
DENNICK, DONNA
09012001
CHEVROLET
0656025
1995
0
46424-0121
2050
DERBIN, DEANNA
09022001
PIERCE-ARROW
1640025
1937
2
46375-3110
2065
DONNEHUE, PHILLIP
09042001
STUDEBAKER
0054000
1966
1
46405-1291
2085
DOPPLER, RADAR O.
08302001
HUDSON
0311000
1951
1
46310-1234
2091
DUNLOP, RITA
08252001
FORD
1450045
1996
2
46303-1201
3003
DYKES, CYNTHIA
08262001
FORD
0410050
1987
1
46413-2311
3011
EATON, ESTER B.
09012001
CHEVROLET
1356050
1996
0
46404-0121
3015
EFFLEY, BAILY
09022001
PIERCE-ARROW
1446045
1933
2
46307-3110
3025
EGGERTON, AMANDA
08292001
STUDEBAKER
0335000
1965
1
46410-1291
3026
EPPLEY, DAVID
08282001
FORD
0414000
1995
1
46342-1234
3031
ERKLE, ROSA
09032001
FORD
0355020
1993
2
46307-1211
3043
FARNSWORTH, WESLEY
09052001
DODGE BROS.
1150000
1996
0
46410-2311
3047
FLANNERY, JAMES
09012001
CHEVROLET
0450050
1992
0
46410-0121
3050
FOREMAN, OTTO J.
09022001
PIERCE-ARROW
0940000
1927
2
46305-3111
3055
FOWLER, KATHLEEN
08272001
STUDEBAKER
0267050
1964
1
46410-1221
3056
FURNACE, DAVID
08292001
HUDSON
0530000
1948
1
46414-1231
3061
GALLAGHER, CLARENCE
09042001
FORD
1006000
1992
2
46307-1201
3062
GENNERRO, TONY S.
08302001
CHEVROLET
0320050
1989
0
46421-2311
3066
GOEBEL, NANCY K.
09022001
CHEVROLET
0643050
1990
0
46323-0121
3072
GUNTHER, FREDERICK
09032001
PIERCE-ARROW
1260000
1930
2
46315-3110
3080
HAINES, MARSHALL
08292001
STUDEBAKER
02500251
959
1
46425-1291
3094
HANCOCK, JONATHON
09032001
HUDSON
0330000
1953
1
46423-2311
3098
HARTNETT, ROBERTO
09052001
CHEVROLET
0326040
1988
0
46424-0121
4005
HENNING, SONIA
09052001
PIERCE-ARROW
1305000
1928
2
46375-3110
4009
HORNSBY, ROGERS
08252001
STUDEBAKER
0167500
1962
1
46405-1291
4012
HYATT, JANET F.
08302001
HUDSON
0155000
1951
1
46301-1234
4021
IDZIOR, RAYMOND
09012001
FORD
1460000
1996
2
46303-1201
4022
JENNINGS, WILLIAM
08262001
FORD
0612040
1992
1
46410-2311
4024
JOHNSON, JACK
09032001
CHEVROLET
0256050
1985
0
46404-0121
4032
KULKA, ROBERT C.
09042001
PIERCE-ARROW
0970000
1934
2
46307-3110
4035
KURTZ, DONALD
08302001
STUDEBAKER
0345000
1966
1
46410-1291
4038
LEVANDOWSKI, JILL
08282001
DODGE BROS.
0430000
1988
1
46342-1234
4044
METZ, ARNOLD E.
09012001
FORD
1323000
1996
2
46307-1211
4046
NORRIS, CHARLES S.
09052001
CHEVROLET
0844000
1992
0
46410-2311
4047
NOWAKOWSKI, ALFRED
09012001
DODGE BROS.
0656050
1994
1
46410-0121
4053
O'BOYLE, NIEL
09032001
PIERCE-ARROW
1550000
1938
2
46305-3111
4056
O'BRIAN, PATRICK
08302001
STUDEBAKER
0347050
1962
1
46410-1221
4059
PATTERSON, LENNI R.
08292001
HUDSON
0250000
1946
1
46414-1231
4061
PERRY, SHAMUS
09052001
FORD
0895000
1994
2
46307-1201
4066
REED, ROBERT B.
08272001
CHEVROLET
0740050
1996
0
46421-2311
4067
RODRIGUEZ, ALONZO
09052001
DODGE BROS.
1050050
1995
0
46323-0121
4073
SANCHEZ, HENRY
09012001
PIERCE-ARROW
0830000
1925
2
46315-3110
4081
SWARTZ, HECTOR
08282001
STUDEBAKER
0075025
1954
1
46301-1234
4084
TORREZ, MARTIN
08272001
FORD
0565000
1994
2
46303-1201
4090
TUTTLE, MARK
08292001
FORD
0710050
1996
1
46413-2311
4094
WARNER, JACK
09042001
CHEVROLET
0856050
1996
0
46404-0121
4115
YACKLEY, YOURTO
09042001
PIERCE-ARROW
1000000
1930
2
谢谢。
我想你可以使用 TableView
和 Pagination
的组合,就像这篇文章中描述的那样:JavaFX TableView Paginator
这是一个例子:
App.java:
package org.example;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
public class App extends Application {
ObservableList<CustomerSale> data = FXCollections.observableArrayList(readFromFile());
public int itemsPerPage() {
return 1;
}
public int rowsPerPage() {
return 30;
}
public VBox createPage(int pageIndex) {
int lastIndex;
int displace = data.size() % rowsPerPage();
if (displace > 0) {
lastIndex = data.size() / rowsPerPage();
} else {
lastIndex = data.size() / rowsPerPage() - 1;
}
VBox box = new VBox(5);
int page = pageIndex * itemsPerPage();
for (int i = page; i < page + itemsPerPage(); i++) {
// Create table and its columns:
TableView<CustomerSale> table = new TableView<>();
TableColumn<CustomerSale, String> headerCol = new TableColumn<>("ABC USED CARS");
TableColumn<CustomerSale, String> zipCodeExtensionCol = new TableColumn<>("ZIP CODE-EXT");
TableColumn<CustomerSale, String> customerNameCol = new TableColumn<>("CUSTOMER NAME");
TableColumn<CustomerSale, String> makeCol = new TableColumn<>("MAKE");
TableColumn<CustomerSale, Integer> customerNumberCol = new TableColumn<>("CUST No.");
TableColumn<CustomerSale, Integer> purchaseDateCol = new TableColumn<>("DoP");
TableColumn<CustomerSale, Integer> purchasePriceCol = new TableColumn<>("PRICE");
TableColumn<CustomerSale, Integer> yearOfVehicleCol = new TableColumn<>("YEAR");
TableColumn<CustomerSale, Integer> satisfactionRatingCol = new TableColumn<>("RATE");
headerCol.getColumns().addAll(Arrays.asList(zipCodeExtensionCol, customerNumberCol, customerNameCol, purchaseDateCol,
makeCol, purchasePriceCol, yearOfVehicleCol, satisfactionRatingCol));
table.getColumns().add(headerCol);
// Set value factories:
zipCodeExtensionCol.setCellValueFactory(cellData -> cellData.getValue().zipCodeExtensionProperty());
customerNameCol.setCellValueFactory(cellData -> cellData.getValue().customerNameProperty());
makeCol.setCellValueFactory(cellData -> cellData.getValue().makeProperty());
customerNumberCol.setCellValueFactory(cellData -> cellData.getValue().customerNumberProperty().asObject());
purchaseDateCol.setCellValueFactory(cellData -> cellData.getValue().purchaseDateProperty().asObject());
purchasePriceCol.setCellValueFactory(cellData -> cellData.getValue().purchasePriceProperty().asObject());
yearOfVehicleCol.setCellValueFactory(cellData -> cellData.getValue().yearOfVehicleProperty().asObject());
satisfactionRatingCol.setCellValueFactory(cellData -> cellData.getValue().satisfactionRatingProperty().asObject());
// Custom cell for currency:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat df = (DecimalFormat) nf;
df.applyPattern("¤###,###");
purchasePriceCol.setCellFactory(tc -> new TableCell<>() {
@Override
protected void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if ((item == null || empty))
setText(null);
else
setText(df.format(item.intValue()));
}
});
if (lastIndex == pageIndex)
table.setItems(FXCollections.observableArrayList(data.subList(pageIndex
* rowsPerPage(), pageIndex * rowsPerPage() + displace)));
else
table.setItems(FXCollections.observableArrayList(data.subList(pageIndex
* rowsPerPage(), pageIndex * rowsPerPage() + rowsPerPage())));
box.getChildren().add(table);
}
return box;
}
@Override
public void start(Stage stage) {
Pagination pagination = new Pagination((data.size() / rowsPerPage() + 1), 0);
pagination.setPageFactory(pageIndex -> {
if (pageIndex > data.size() / rowsPerPage() + 1)
return null;
else
return createPage(pageIndex);
});
stage.setScene(new Scene(pagination));
stage.show();
}
/**
* Get the data from the text file.
*
* @return list with data
*/
private List<CustomerSale> readFromFile() {
List<CustomerSale> result = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(Objects.requireNonNull(App.class.getResource(
"/org/example/data.txt")).getFile()))) {
String line;
int counter = 0;
CustomerSale customerSale = new CustomerSale();
while ((line = reader.readLine()) != null) {
switch (counter) {
case 0 -> customerSale.setZipCodeExtension(line);
case 1 -> customerSale.setCustomerNumber(Integer.parseInt(line));
case 2 -> customerSale.setCustomerName(line);
case 3 -> customerSale.setPurchaseDate(Integer.parseInt(line));
case 4 -> customerSale.setMake(line);
case 5 -> customerSale.setPurchasePrice(Integer.parseInt(line));
case 6 -> customerSale.setYearOfVehicle(Integer.parseInt(line));
case 7 -> customerSale.setSatisfactionRating(Integer.parseInt(line));
}
++counter;
if (counter == 8) {
result.add(customerSale);
customerSale = new CustomerSale();
counter = 0;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
launch();
}
}
CustomerSale.java:
package org.example;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class CustomerSale {
private final StringProperty
zipCodeExtension,
customerName,
make;
private final IntegerProperty
customerNumber,
purchaseDate,
purchasePrice,
yearOfVehicle,
satisfactionRating;
public CustomerSale() {
zipCodeExtension = new SimpleStringProperty("");
customerName = new SimpleStringProperty("");
make = new SimpleStringProperty("");
customerNumber = new SimpleIntegerProperty(-1);
purchaseDate = new SimpleIntegerProperty(-1);
purchasePrice = new SimpleIntegerProperty(-1);
yearOfVehicle = new SimpleIntegerProperty(-1);
satisfactionRating = new SimpleIntegerProperty(-1);
}
public CustomerSale(String zipCodeExtension,
int customerNumber,
String customerName,
int purchaseDate,
String make,
int purchasePrice,
int yearOfVehicle,
int satisfactionRating) {
this();
setZipCodeExtension(zipCodeExtension);
setCustomerNumber(customerNumber);
setCustomerName(customerName);
setPurchaseDate(purchaseDate);
setMake(make);
setPurchasePrice(purchasePrice);
setYearOfVehicle(yearOfVehicle);
setSatisfactionRating(satisfactionRating);
}
public String getZipCodeExtension() {
return zipCodeExtension.get();
}
public StringProperty zipCodeExtensionProperty() {
return zipCodeExtension;
}
public void setZipCodeExtension(String zipCodeExtension) {
this.zipCodeExtension.set(zipCodeExtension);
}
public String getCustomerName() {
return customerName.get();
}
public StringProperty customerNameProperty() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName.set(customerName);
}
public String getMake() {
return make.get();
}
public StringProperty makeProperty() {
return make;
}
public void setMake(String make) {
this.make.set(make);
}
public int getCustomerNumber() {
return customerNumber.get();
}
public IntegerProperty customerNumberProperty() {
return customerNumber;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber.set(customerNumber);
}
public int getPurchaseDate() {
return purchaseDate.get();
}
public IntegerProperty purchaseDateProperty() {
return purchaseDate;
}
public void setPurchaseDate(int purchaseDate) {
this.purchaseDate.set(purchaseDate);
}
public int getPurchasePrice() {
return purchasePrice.get();
}
public IntegerProperty purchasePriceProperty() {
return purchasePrice;
}
public void setPurchasePrice(int purchasePrice) {
this.purchasePrice.set(purchasePrice);
}
public int getYearOfVehicle() {
return yearOfVehicle.get();
}
public IntegerProperty yearOfVehicleProperty() {
return yearOfVehicle;
}
public void setYearOfVehicle(int yearOfVehicle) {
this.yearOfVehicle.set(yearOfVehicle);
}
public int getSatisfactionRating() {
return satisfactionRating.get();
}
public IntegerProperty satisfactionRatingProperty() {
return satisfactionRating;
}
public void setSatisfactionRating(int satisfactionRating) {
this.satisfactionRating.set(satisfactionRating);
}
}
预览:
我想使用 JavaFX 显示来自 .txt 报告文件的数据。在我的代码中,我尝试使用 Labels 和 Vbox 在 GUI 场景中以多种格式显示信息。但是,我在将结果输出为 GUI 而不是控制台时遇到了很糟糕的情况。我试图研究我的问题,但找不到解决问题所需的信息。
这是我需要使用 JavaFX 显示为 GUI 应用程序的报告:
这是我的代码显示为 GUI 的内容:
这是我的源代码:
package sample;
public class CustomerSale {
// Details of reports
private String zipCodeExtension;
private int customerNumber;
private String customerName;
private int purchaseDate;
private String make;
private int purchasePrice;
private int yearOfVehicle;
private int satisfactionRating;
// Create constructor argument
public CustomerSale(String zipCodeExtension, int customerNumber, String customerName,
int purchaseDate, String make, int purchasePrice,
int yearOfVehicle, int satisfactionRating) {
this.zipCodeExtension = zipCodeExtension;
this.customerNumber = customerNumber;
this.customerName = customerName;
this.purchaseDate = purchaseDate;
this.make = make;
this.purchasePrice = purchasePrice;
this.yearOfVehicle = yearOfVehicle;
this.satisfactionRating = satisfactionRating;
}
// Create getters and setters
public String getMake() {
return make;
}
public String getZipCodeExtension() {
return zipCodeExtension;
}
public int getCustomerNumber() {
return customerNumber;
}
public String getCustomerName() {
return customerName;
}
public int getPurchaseDate() {
return purchaseDate;
}
public double getPurchasePrice() {
return purchasePrice;
}
public int getYearOfVehicle() {
return yearOfVehicle;
}
public int getSatisfactionRating() {
return satisfactionRating;
}
}
package sample;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.File;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
/**
ABC used cars report Application
*/
public class FinalGUIOutput extends Application {
final int WINDOW_WIDTH = 750, // Window width in pixels
WINDOW_HEIGHT = 150; // Window height in pixels
public static void main(String[] args) {
// Launch the application
launch(args);
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
// Create a Label to display the heading and page number.
Label headingLabel = new Label("---------------------------------------------------------ABC USED CARS--------------------------------------------------------");
Label pageNumber = new Label(
"-------------------------------------------------------PAGE 1-----------------------------------------------------------------");
// Label pageFormat = new Label("%-19s%-15s%-21s%-15s%-20s%-17s%-12s%-20s\n", "|ZIP CODE-EXT|", "|CUST No.|", "|CUSTOMER NAME|", "|DoP|",
// "|MAKE|", "|PRICE|", "|YEAR|", "|RATE|");
Label pageLine = new Label(
"------------------------------------------------------------------------------------------------------------------------------");
// Create an empty Label to display the result.
Label resultLabel = new Label();
File carsInfo = new File("custsale.txt");
Scanner carsScanner = new Scanner(carsInfo);
String format = "%-20s%-14s%-21s%-16s%-19s$%-17s%-14s%-20s\n";
final int pageSize = 30;
int carCounter = 0;
while (carsScanner.hasNextLine()) {
if (carCounter % pageSize == 0) {
printPageHeader(carCounter / pageSize + 1);
}
// Read the carsScanner
String zipCodeExtension = carsScanner.nextLine();
int customerNumber = Integer.parseInt(carsScanner.nextLine());
String customerName = carsScanner.nextLine();
int purchaseDate = Integer.parseInt(carsScanner.nextLine());
String make = carsScanner.nextLine();
int purchasePrice = Integer.parseInt(carsScanner.nextLine());
int yearOfVehicle = Integer.parseInt(carsScanner.nextLine());
int satisfactionRating = Integer.parseInt(carsScanner.nextLine());
CustomerSale custsale = new CustomerSale(zipCodeExtension, customerNumber, customerName,
purchaseDate, make, purchasePrice, yearOfVehicle, satisfactionRating);
// Display the report
DecimalFormat df = new DecimalFormat("#,#####");
System.out.printf(format, custsale.getZipCodeExtension(), custsale.getCustomerNumber(),
custsale.getCustomerName(), custsale.getPurchaseDate(), custsale.getMake(),
df.format(custsale.getPurchasePrice()), custsale.getYearOfVehicle(),
custsale.getSatisfactionRating());
// Increment the car counter
carCounter++;
}
carsScanner.close();
/**
GUI Display
*/
// Put the HBox, calcButton and resultLabel in a VBox.
VBox vbox = new VBox(10,headingLabel, pageNumber, pageLine, resultLabel);
// Set the VBox's alignment to center.
vbox.setAlignment(Pos.CENTER);
// Set the VBox's padding to 10 pixels
vbox.setPadding(new Insets(10));
// Create a Scene with the VBox as its root node.
Scene scene = new Scene(vbox, WINDOW_WIDTH, WINDOW_HEIGHT);
// set the scene's alignment to center.
vbox.setAlignment(Pos.CENTER);
//Add the Scene to the Stage
primaryStage.setScene(scene);
// Set the stage title
primaryStage.setTitle("ABC Used Cars");
// Show the window
primaryStage.show();
}
private static void printPageHeader(int i) {
// Create a Label to display the heading and page number.
Label headingLabel = new Label("---------------------------------------------------------ABC USED CARS--------------------------------------------------------");
Label pageNumber = new Label(
"-------------------------------------------------------" + "PAGE " + i + "-----------------------------------------------------------------");
// Label pageFormat = new Label("%-19s%-15s%-21s%-15s%-20s%-17s%-12s%-20s\n", "|ZIP CODE-EXT|", "|CUST No.|", "|CUSTOMER NAME|", "|DoP|",
// "|MAKE|", "|PRICE|", "|YEAR|", "|RATE|");
Label pageLine = new Label(
"------------------------------------------------------------------------------------------------------------------------------");
}
}
最后这是我试图从中提取数据的 .txt 文件报告:
46410-1234
1001
ALBERT, CARL T.
08252001
FORD
0255000
1991
2
46307-1201
1003
ANDREWS, ROBERT
08262001
CHEVROLET
0700000
1958
0
46423-2311
1008
ANZIO, RAFELINO
09012001
CHEVROLET
0456050
1978
0
46424-0121
1010
ASHLEY, WILLIAM B.
09022001
PIERCE-ARROW
1240000
1932
2
46375-3110
1015
ATKINSON, MARK
08272001
STUDEBAKER
0050000
1958
1
46405-1291
1025
AVERY, ALFRED A.
08292001
HUDSON
0230000
1954
1
46301-1234
1031
BEZZMEK, JENNIFER
08272001
FORD
0455000
1995
2
46303-1201
1033
BLAKE, DONALD
08292001
FORD
0722050
1989
1
46413-2311
1041
BLONDELL, BONNIE
09042001
CHEVROLET
0356050
1988
1
46404-0121
1045
BONADIO, JAMES
09032001
PIERCE-ARROW
1150000
1935
2
46307-3110
1055
BUCKO, ONIEDA
08282001
STUDEBAKER
0245000
1964
1
46410-1291
1056
BYMANN, FREDRICK
08292001
FORD
0330000
1994
1
46342-1234
1061
CALBERT, RONALD
09052001
FORD
0355000
1993
2
46307-1211
1063
CHELSEA, MARTHA S.
08302001
DODGE BROS.
0350000
1935
0
46410-2311
1067
CLAFLIN, WAYNE R.
09012001
CHEVROLET
0456050
1990
2
46410-0121
1070
COLE, CHARLES C.
09022001
PIERCE-ARROW
1050000
1929
2
46305-3111
1075
COLEMAN, THOMAS
08272001
STUDEBAKER
0167050
1961
1
46410-1221
1076
COLWELL, RICHARD L.
08292001
HUDSON
0430000
1940
1
46414-1231
1080
COOPER, JOHNATHAN
09052001
FORD
1256000
1996
2
46307-1201
2002
COREY, SARAH D.
08272001
CHEVROLET
0650050
1994
0
46421-2311
2004
CRACKLIN, GOODMAN
09012001
CHEVROLET
1456050
1996
0
46323-0121
2011
CRAWFORD, TIMOTHY
09012001
PIERCE-ARROW
1040000
1931
2
46315-3110
2012
CURRIE, RAYMOND
08282001
STUDEBAKER
0150025
1956
1
46425-1291
2024
CYBORG, IZORE M.
09052001
HUDSON
0130000
1949
1
46410-1234
2031
DALTON, DAVID P.
08252001
FORD
0234000
1990
2
46307-1201
2043
DAVIES, RALPH O.
08262001
CHEVROLET
0333000
1989
0
46423-2311
2048
DENNICK, DONNA
09012001
CHEVROLET
0656025
1995
0
46424-0121
2050
DERBIN, DEANNA
09022001
PIERCE-ARROW
1640025
1937
2
46375-3110
2065
DONNEHUE, PHILLIP
09042001
STUDEBAKER
0054000
1966
1
46405-1291
2085
DOPPLER, RADAR O.
08302001
HUDSON
0311000
1951
1
46310-1234
2091
DUNLOP, RITA
08252001
FORD
1450045
1996
2
46303-1201
3003
DYKES, CYNTHIA
08262001
FORD
0410050
1987
1
46413-2311
3011
EATON, ESTER B.
09012001
CHEVROLET
1356050
1996
0
46404-0121
3015
EFFLEY, BAILY
09022001
PIERCE-ARROW
1446045
1933
2
46307-3110
3025
EGGERTON, AMANDA
08292001
STUDEBAKER
0335000
1965
1
46410-1291
3026
EPPLEY, DAVID
08282001
FORD
0414000
1995
1
46342-1234
3031
ERKLE, ROSA
09032001
FORD
0355020
1993
2
46307-1211
3043
FARNSWORTH, WESLEY
09052001
DODGE BROS.
1150000
1996
0
46410-2311
3047
FLANNERY, JAMES
09012001
CHEVROLET
0450050
1992
0
46410-0121
3050
FOREMAN, OTTO J.
09022001
PIERCE-ARROW
0940000
1927
2
46305-3111
3055
FOWLER, KATHLEEN
08272001
STUDEBAKER
0267050
1964
1
46410-1221
3056
FURNACE, DAVID
08292001
HUDSON
0530000
1948
1
46414-1231
3061
GALLAGHER, CLARENCE
09042001
FORD
1006000
1992
2
46307-1201
3062
GENNERRO, TONY S.
08302001
CHEVROLET
0320050
1989
0
46421-2311
3066
GOEBEL, NANCY K.
09022001
CHEVROLET
0643050
1990
0
46323-0121
3072
GUNTHER, FREDERICK
09032001
PIERCE-ARROW
1260000
1930
2
46315-3110
3080
HAINES, MARSHALL
08292001
STUDEBAKER
02500251
959
1
46425-1291
3094
HANCOCK, JONATHON
09032001
HUDSON
0330000
1953
1
46423-2311
3098
HARTNETT, ROBERTO
09052001
CHEVROLET
0326040
1988
0
46424-0121
4005
HENNING, SONIA
09052001
PIERCE-ARROW
1305000
1928
2
46375-3110
4009
HORNSBY, ROGERS
08252001
STUDEBAKER
0167500
1962
1
46405-1291
4012
HYATT, JANET F.
08302001
HUDSON
0155000
1951
1
46301-1234
4021
IDZIOR, RAYMOND
09012001
FORD
1460000
1996
2
46303-1201
4022
JENNINGS, WILLIAM
08262001
FORD
0612040
1992
1
46410-2311
4024
JOHNSON, JACK
09032001
CHEVROLET
0256050
1985
0
46404-0121
4032
KULKA, ROBERT C.
09042001
PIERCE-ARROW
0970000
1934
2
46307-3110
4035
KURTZ, DONALD
08302001
STUDEBAKER
0345000
1966
1
46410-1291
4038
LEVANDOWSKI, JILL
08282001
DODGE BROS.
0430000
1988
1
46342-1234
4044
METZ, ARNOLD E.
09012001
FORD
1323000
1996
2
46307-1211
4046
NORRIS, CHARLES S.
09052001
CHEVROLET
0844000
1992
0
46410-2311
4047
NOWAKOWSKI, ALFRED
09012001
DODGE BROS.
0656050
1994
1
46410-0121
4053
O'BOYLE, NIEL
09032001
PIERCE-ARROW
1550000
1938
2
46305-3111
4056
O'BRIAN, PATRICK
08302001
STUDEBAKER
0347050
1962
1
46410-1221
4059
PATTERSON, LENNI R.
08292001
HUDSON
0250000
1946
1
46414-1231
4061
PERRY, SHAMUS
09052001
FORD
0895000
1994
2
46307-1201
4066
REED, ROBERT B.
08272001
CHEVROLET
0740050
1996
0
46421-2311
4067
RODRIGUEZ, ALONZO
09052001
DODGE BROS.
1050050
1995
0
46323-0121
4073
SANCHEZ, HENRY
09012001
PIERCE-ARROW
0830000
1925
2
46315-3110
4081
SWARTZ, HECTOR
08282001
STUDEBAKER
0075025
1954
1
46301-1234
4084
TORREZ, MARTIN
08272001
FORD
0565000
1994
2
46303-1201
4090
TUTTLE, MARK
08292001
FORD
0710050
1996
1
46413-2311
4094
WARNER, JACK
09042001
CHEVROLET
0856050
1996
0
46404-0121
4115
YACKLEY, YOURTO
09042001
PIERCE-ARROW
1000000
1930
2
谢谢。
我想你可以使用 TableView
和 Pagination
的组合,就像这篇文章中描述的那样:JavaFX TableView Paginator
这是一个例子:
App.java:
package org.example;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
public class App extends Application {
ObservableList<CustomerSale> data = FXCollections.observableArrayList(readFromFile());
public int itemsPerPage() {
return 1;
}
public int rowsPerPage() {
return 30;
}
public VBox createPage(int pageIndex) {
int lastIndex;
int displace = data.size() % rowsPerPage();
if (displace > 0) {
lastIndex = data.size() / rowsPerPage();
} else {
lastIndex = data.size() / rowsPerPage() - 1;
}
VBox box = new VBox(5);
int page = pageIndex * itemsPerPage();
for (int i = page; i < page + itemsPerPage(); i++) {
// Create table and its columns:
TableView<CustomerSale> table = new TableView<>();
TableColumn<CustomerSale, String> headerCol = new TableColumn<>("ABC USED CARS");
TableColumn<CustomerSale, String> zipCodeExtensionCol = new TableColumn<>("ZIP CODE-EXT");
TableColumn<CustomerSale, String> customerNameCol = new TableColumn<>("CUSTOMER NAME");
TableColumn<CustomerSale, String> makeCol = new TableColumn<>("MAKE");
TableColumn<CustomerSale, Integer> customerNumberCol = new TableColumn<>("CUST No.");
TableColumn<CustomerSale, Integer> purchaseDateCol = new TableColumn<>("DoP");
TableColumn<CustomerSale, Integer> purchasePriceCol = new TableColumn<>("PRICE");
TableColumn<CustomerSale, Integer> yearOfVehicleCol = new TableColumn<>("YEAR");
TableColumn<CustomerSale, Integer> satisfactionRatingCol = new TableColumn<>("RATE");
headerCol.getColumns().addAll(Arrays.asList(zipCodeExtensionCol, customerNumberCol, customerNameCol, purchaseDateCol,
makeCol, purchasePriceCol, yearOfVehicleCol, satisfactionRatingCol));
table.getColumns().add(headerCol);
// Set value factories:
zipCodeExtensionCol.setCellValueFactory(cellData -> cellData.getValue().zipCodeExtensionProperty());
customerNameCol.setCellValueFactory(cellData -> cellData.getValue().customerNameProperty());
makeCol.setCellValueFactory(cellData -> cellData.getValue().makeProperty());
customerNumberCol.setCellValueFactory(cellData -> cellData.getValue().customerNumberProperty().asObject());
purchaseDateCol.setCellValueFactory(cellData -> cellData.getValue().purchaseDateProperty().asObject());
purchasePriceCol.setCellValueFactory(cellData -> cellData.getValue().purchasePriceProperty().asObject());
yearOfVehicleCol.setCellValueFactory(cellData -> cellData.getValue().yearOfVehicleProperty().asObject());
satisfactionRatingCol.setCellValueFactory(cellData -> cellData.getValue().satisfactionRatingProperty().asObject());
// Custom cell for currency:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat df = (DecimalFormat) nf;
df.applyPattern("¤###,###");
purchasePriceCol.setCellFactory(tc -> new TableCell<>() {
@Override
protected void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if ((item == null || empty))
setText(null);
else
setText(df.format(item.intValue()));
}
});
if (lastIndex == pageIndex)
table.setItems(FXCollections.observableArrayList(data.subList(pageIndex
* rowsPerPage(), pageIndex * rowsPerPage() + displace)));
else
table.setItems(FXCollections.observableArrayList(data.subList(pageIndex
* rowsPerPage(), pageIndex * rowsPerPage() + rowsPerPage())));
box.getChildren().add(table);
}
return box;
}
@Override
public void start(Stage stage) {
Pagination pagination = new Pagination((data.size() / rowsPerPage() + 1), 0);
pagination.setPageFactory(pageIndex -> {
if (pageIndex > data.size() / rowsPerPage() + 1)
return null;
else
return createPage(pageIndex);
});
stage.setScene(new Scene(pagination));
stage.show();
}
/**
* Get the data from the text file.
*
* @return list with data
*/
private List<CustomerSale> readFromFile() {
List<CustomerSale> result = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(Objects.requireNonNull(App.class.getResource(
"/org/example/data.txt")).getFile()))) {
String line;
int counter = 0;
CustomerSale customerSale = new CustomerSale();
while ((line = reader.readLine()) != null) {
switch (counter) {
case 0 -> customerSale.setZipCodeExtension(line);
case 1 -> customerSale.setCustomerNumber(Integer.parseInt(line));
case 2 -> customerSale.setCustomerName(line);
case 3 -> customerSale.setPurchaseDate(Integer.parseInt(line));
case 4 -> customerSale.setMake(line);
case 5 -> customerSale.setPurchasePrice(Integer.parseInt(line));
case 6 -> customerSale.setYearOfVehicle(Integer.parseInt(line));
case 7 -> customerSale.setSatisfactionRating(Integer.parseInt(line));
}
++counter;
if (counter == 8) {
result.add(customerSale);
customerSale = new CustomerSale();
counter = 0;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
launch();
}
}
CustomerSale.java:
package org.example;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class CustomerSale {
private final StringProperty
zipCodeExtension,
customerName,
make;
private final IntegerProperty
customerNumber,
purchaseDate,
purchasePrice,
yearOfVehicle,
satisfactionRating;
public CustomerSale() {
zipCodeExtension = new SimpleStringProperty("");
customerName = new SimpleStringProperty("");
make = new SimpleStringProperty("");
customerNumber = new SimpleIntegerProperty(-1);
purchaseDate = new SimpleIntegerProperty(-1);
purchasePrice = new SimpleIntegerProperty(-1);
yearOfVehicle = new SimpleIntegerProperty(-1);
satisfactionRating = new SimpleIntegerProperty(-1);
}
public CustomerSale(String zipCodeExtension,
int customerNumber,
String customerName,
int purchaseDate,
String make,
int purchasePrice,
int yearOfVehicle,
int satisfactionRating) {
this();
setZipCodeExtension(zipCodeExtension);
setCustomerNumber(customerNumber);
setCustomerName(customerName);
setPurchaseDate(purchaseDate);
setMake(make);
setPurchasePrice(purchasePrice);
setYearOfVehicle(yearOfVehicle);
setSatisfactionRating(satisfactionRating);
}
public String getZipCodeExtension() {
return zipCodeExtension.get();
}
public StringProperty zipCodeExtensionProperty() {
return zipCodeExtension;
}
public void setZipCodeExtension(String zipCodeExtension) {
this.zipCodeExtension.set(zipCodeExtension);
}
public String getCustomerName() {
return customerName.get();
}
public StringProperty customerNameProperty() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName.set(customerName);
}
public String getMake() {
return make.get();
}
public StringProperty makeProperty() {
return make;
}
public void setMake(String make) {
this.make.set(make);
}
public int getCustomerNumber() {
return customerNumber.get();
}
public IntegerProperty customerNumberProperty() {
return customerNumber;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber.set(customerNumber);
}
public int getPurchaseDate() {
return purchaseDate.get();
}
public IntegerProperty purchaseDateProperty() {
return purchaseDate;
}
public void setPurchaseDate(int purchaseDate) {
this.purchaseDate.set(purchaseDate);
}
public int getPurchasePrice() {
return purchasePrice.get();
}
public IntegerProperty purchasePriceProperty() {
return purchasePrice;
}
public void setPurchasePrice(int purchasePrice) {
this.purchasePrice.set(purchasePrice);
}
public int getYearOfVehicle() {
return yearOfVehicle.get();
}
public IntegerProperty yearOfVehicleProperty() {
return yearOfVehicle;
}
public void setYearOfVehicle(int yearOfVehicle) {
this.yearOfVehicle.set(yearOfVehicle);
}
public int getSatisfactionRating() {
return satisfactionRating.get();
}
public IntegerProperty satisfactionRatingProperty() {
return satisfactionRating;
}
public void setSatisfactionRating(int satisfactionRating) {
this.satisfactionRating.set(satisfactionRating);
}
}
预览: