(已编辑)尝试制作一个购物车,当我单击一本书时,它会从文本文件中获取书名,然后获取值并将其添加或删除到总计中。
目前,我还没有所有代码,只是要从文本文件中提取文本信息。当我当前启动程序时,它将“ Null”值打印到屏幕上,而不是书名
原始工作文件
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();
}
}
在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
问题来源:Stack Overflow
操作顺序错误。
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);
});
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。