[英]UI printing Null value instead of text from txt file
(Edited) Trying to make a shopping cart that when I click on a book, it takes the book title from the text file and then takes the value and adds or deletes it to the total. (已编辑)尝试制作一个购物车,当我点击一本书时,它会从文本文件中获取书名,然后获取该值并将其添加或删除到总数中。
At the current moment, I do not have all the code, just working on pulling the text information in from the text file.目前,我没有所有代码,只是从文本文件中提取文本信息。 and when I currently start up the program it prints out "Null" value to the screen instead of the book titles
当我当前启动程序时,它会在屏幕上打印出“空”值而不是书名
Original Working file原始工作文件
package shoppingcart2;
import java.awt.BorderLayout;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.ListView;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.GridPane;
import javafx.scene.transform.Scale;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
public class ShoppingCart2 extends Application
{
private JPanel listPanel;
private JPanel shoppingPanel;
private JPanel buttonsPanel;
private JList listItems;
private JButton addButton;
private JButton removeButton;
private JButton clearButton;
private JButton checkoutButton;
private String[] listArray = new String[7];
private java.awt.List cartItems = new java.awt.List();
private final double salesTax = 0.06;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException
{
//list view items book
ListView<String> listView = new ListView<>();
listView.setPrefSize(200, 170);
listView.getItems().addAll(listArray);
// create label to display the selection
Label selectedNameLabel = new Label("Select a Book");
//Button for selection
Button getButton = new Button("Add to Cart");
//Event for Add to cart button
getButton.setOnAction(event ->
{
//Get book
String selected = listView.getSelectionModel().getSelectedItem();
//Display selected item in label
selectedNameLabel.setText(selected);
});
String line;
int index = 0;
File file = new File("BookPrices.txt");
Scanner fileReader = new Scanner(file);
while(fileReader.hasNext())
{
line = fileReader.nextLine();
String [] titles = line.split(",");
listArray[index] = titles[0];
index++;
}
//Controls to VBox
VBox vbox = new VBox(10, listView, selectedNameLabel, getButton);
vbox.setPadding(new Insets(10));
vbox.setAlignment(Pos.CENTER);
//Show
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Inside the BookPrices file在 BookPrices 文件中
I Did It Your Way, 11.95
The History of Scotland, 14.50
Learn Calculus in One Day, 29.95
Feel the Stress, 18.50
Great Poems, 12.95
Europe on a Shoestring, 10.95
The Life of Mozart, 14.50
order of operations mistake.操作顺序错误。
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException
{
String line;
int index = 0;
File file = new File("BookPrices.txt");
Scanner fileReader = new Scanner(file);
while(fileReader.hasNext())
{
line = fileReader.nextLine();
String [] titles = line.split(",");
listArray[index] = titles[0];
index++;
}
//list view items book
ListView<String> listView = new ListView<>();
listView.setPrefSize(200, 170);
listView.getItems().addAll(listArray);
// create label to display the selection
Label selectedNameLabel = new Label("Select a Book");
//Button for selection
Button getButton = new Button("Add to Cart");
//Event for Add to cart button
getButton.setOnAction(event ->
{
//Get book
String selected = listView.getSelectionModel().getSelectedItem();
//Display selected item in label
selectedNameLabel.setText(selected);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.