开发者社区> 问答> 正文

UI打印Null值,而不是txt文件中的文本

(已编辑)尝试制作一个购物车,当我单击一本书时,它会从文本文件中获取书名,然后获取值并将其添加或删除到总计中。

目前,我还没有所有代码,只是要从文本文件中提取文本信息。当我当前启动程序时,它将“ 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

展开
收起
montos 2020-03-27 16:42:35 648 0
1 条回答
写回答
取消 提交回答
  • 操作顺序错误。

        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

    2020-03-27 16:43:02
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用TensorFlow搭建智能开发系统自劢生成App UI代码 立即下载
Fusion Design - 企业级UI解决方案揭秘 立即下载
使用TensorFlow搭建智能开发系统自动生成App UI 立即下载