因此,我花了一个小时试图弄清楚如何从GUI打开这个文件,但每次我点击打开的文件,然后单击他想要打开的文件,我的GUI只是崩溃。在这里,我的代码请告诉我什么是错的。 这是给我的,所以它不会错。java打开文件从GUI褴褛阵列双打
import java.io.File;
import java.io.FileNotFoundException;
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
public class MvGuiFx extends Application {;
private double[][] sales;
public static final int MAX_STORES = 6;
public static final int MAX_ITEMS = 6;
private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
Button readFileBtn, exitBtn;
GridPane dataPane;
/**
* Lets the user choose a file to read the sales information and displays
* the information on the screen
* @throws FileNotFoundException
*/
public void readFile() throws FileNotFoundException {
File selectedFile;
FileChooser chooser = new FileChooser();
chooser.setTitle("Choose a file to read retail items' sales information");
if ((selectedFile = chooser.showOpenDialog(null)) != null) {
// Read the file
sales = TwoDimRaggedArrayUtility.readFile(selectedFile);
}
//display on the screen
int row,col;
double total;
for(row=0;row<sales.length; row++)
for(col=0;col<sales[row].length;col++)
dataPane.add(new TextField(currencyFormat.format(sales[row][col])),col+1,row+1);
//display row totals
for(row=0;row<sales.length;row++)
{
total = TwoDimRaggedArrayUtility.getRowTotal(sales, row);
dataPane.add(new TextField(currencyFormat.format(total)), 7, row+1);
}
//find the row with largest number of columns
int columns = 0;
for(row=0;row<sales.length;row++)
if(sales[row].length > columns) columns = sales[row].length;
//display column totals
for(col=0;col<columns;col++)
{
total = TwoDimRaggedArrayUtility.getColumnTotal(sales, col);
dataPane.add(new TextField(currencyFormat.format(total)), col+1, 7);
}
//find highest in each column
for(col=0;col<columns;col++)
{
total = TwoDimRaggedArrayUtility.getHighestInColumn(sales, col);
TextField temp = new TextField(currencyFormat.format(total));
temp.setStyle("-fx-background-color: gray;");
for(row=0;row<sales.length;row++) {
if(col < sales[row].length){
if(sales[row][col]==total)
dataPane.add(temp, col+1, row+1);
}
}
}
}
// Handler class.
private class ButtonEventHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
//handler for Load Sales Data
if (e.getSource() == readFileBtn) {
try {
readFile();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
//handler for Exit button
} else if (e.getSource() == exitBtn)
System.exit(0);
}
}
@Override
public void start(Stage stage) {
Tooltip buttonToolTipArr[] = new Tooltip[5];
buttonToolTipArr[0] = new Tooltip(
"Load sales data from a file and Display");
buttonToolTipArr[1] = new Tooltip("Exit Application");
// Main Pane
BorderPane MainPane = new BorderPane();
// Create Title Pane, add title label and add it to the top of the Main
// Pane
HBox titlePanel = new HBox();
titlePanel.setAlignment(Pos.BASELINE_CENTER);
Label titleLbl = new Label("DisneyWorld District 5 Sales Report\n");
titleLbl.setFont(new Font(30));
titleLbl.setTextFill(Color.BLUE);
titlePanel.getChildren().add(titleLbl);
MainPane.setTop(titlePanel);
// CenterPane
VBox centerPane = new VBox();
centerPane.setAlignment(Pos.CENTER);
// columnHeader Pane
HBox columnHeaderPane = new HBox(10);
columnHeaderPane.setAlignment(Pos.CENTER);
int i,j;
dataPane = new GridPane();
dataPane.setAlignment(Pos.BASELINE_CENTER);
dataPane.add(new Label(" "), 0, 0);
dataPane.add(new Label("Books"), 1, 0);
dataPane.add(new Label("Tsum Tsum"), 2, 0);
dataPane.add(new Label("Trading Pins"), 3, 0);
dataPane.add(new Label("Star Wars"), 4, 0);
dataPane.add(new Label("Lego"), 5, 0);
dataPane.add(new Label("Marvel"), 6, 0);
dataPane.add(new Label("Total"), 7, 0);
for(i=1;i<8;i++)
{
dataPane.add(new Label(" "), 0,i);
for(j = 1; j<8;j++)
dataPane.add(new TextField(), i,j);
}
dataPane.add(new Label("Emporium"), 0, 1);
dataPane.add(new Label("World Traveler"), 0, 2);
dataPane.add(new Label("Discovery Trading Center"), 0, 3);
dataPane.add(new Label("Merchant of Venus"), 0, 4);
dataPane.add(new Label("Once Upon a Toy"), 0, 5);
dataPane.add(new Label("Tatooine Traders"), 0, 6);
dataPane.add(new Label("Total"), 0, 7);
// Create bottom Pane
HBox bottomPane = new HBox(10);
bottomPane.setAlignment(Pos.BASELINE_CENTER);
// Create buttons
readFileBtn = new Button("Load Sales Data");
readFileBtn.setTooltip(buttonToolTipArr[0]);
exitBtn = new Button("Exit");
exitBtn.setTooltip(buttonToolTipArr[1]);
// add event handler to buttons
readFileBtn.setOnAction(new ButtonEventHandler());
exitBtn.setOnAction(new ButtonEventHandler());
// add buttons to bottomPane
bottomPane.getChildren().addAll(readFileBtn, exitBtn);
MainPane.setBottom(bottomPane);
// add panes to center pane
centerPane.getChildren().addAll(dataPane);
MainPane.setCenter(centerPane);
Scene scene = new Scene(MainPane, 1200, 400);
stage.setScene(scene);
// Set stage title and show the stage.
stage.setTitle("District Sales Report");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
继承人是我到目前为止有:
import java.io.PrintWriter;
import java.util.Scanner;
public class TwoDimRaggedArrayUtility {
public TwoDimRaggedArrayUtility()
{
}
public static double[][] readFile(java.io.File file) throws java.io.FileNotFoundException
{
Scanner scan = new Scanner(file);
int row = 0;
while(scan.hasNextLine())
{
row++;
}
double[][] array = new double[row][];
for(int index = 0; index < array[0].length; index++)
array[0] = new double[index];
for(int index = 0; index < array[1].length; index++)
array[1] = new double[index];
for(int index = 0; index < array[2].length; index++)
array[2] = new double[index];
for(int index = 0; index < array[3].length; index++)
array[3] = new double[index];
for(int index = 0; index < array[4].length; index++)
array[4] = new double[index];
for(int index = 0; index < array[5].length; index++)
array[5] = new double[index];
for(int index = 0; index < array[6].length; index++)
array[6] = new double[index];
for(int index = 0; index < array[7].length; index++)
array[7] = new double[index];
for(int index = 0; index < array[8].length; index++)
array[8] = new double[index];
for(int index = 0; index < array[9].length; index++)
array[9] = new double[index];
for(int index = 0; index < array[10].length; index++)
array[10] = new double[index];
scan.close();
return array;
}
public static void writeToFile(double[][] data, java.io.File outputFile)throws java.io.FileNotFoundException
{
PrintWriter print = new PrintWriter(outputFile);
print.print(data);
print.close();
}
而这里的衣衫褴褛的数组.txt文件:
1253.65 4566.50 2154.36 7532.45 3388.44 6598.23
2876.22 3576.24 1954.66
4896.23 2855.29 2386.36 5499.29
2256.76 3623.76 4286.29 5438.48 3794.43
3184.38 3654.65 3455.76 6387.23 4265.77 4592.45
2657.46 3265.34 2256.38 8935.26 5287.34