Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法获取第二个viewlist以将列表作为参数_Java_Javafx - Fatal编程技术网

Java 无法获取第二个viewlist以将列表作为参数

Java 无法获取第二个viewlist以将列表作为参数,java,javafx,Java,Javafx,无法获取第二个创建的视图列表,以接收作为参数添加的“cartItems”值项,无论我如何措辞或在listview中放置什么。尝试使用.getitems(cartItems),但不需要。 他们是否有任何其他的语句或方式,我可以这样说,使添加了添加按钮的idem移动到第二个列表中进行签出 完整代码 package shoppingcart1; import java.io.File; import java.io.FileNotFoundException; import javafx.appli

无法获取第二个创建的视图列表,以接收作为参数添加的“cartItems”值项,无论我如何措辞或在listview中放置什么。尝试使用.getitems(cartItems),但不需要。 他们是否有任何其他的语句或方式,我可以这样说,使添加了添加按钮的idem移动到第二个列表中进行签出

完整代码

package shoppingcart1;
import java.io.File;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
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.ListView;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javax.swing.*;
import java.awt.List;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Scanner;


public class ShoppingCart1 extends Application
{
    private Label answer;
    private Label price;
    ListView <String> listView;
    ListView <String> listView2;
    private String[] listArray = new String[7];
    private String[] listArray2 = new String[7];
    private List cartItems = new List();

    private final double salesTax = 0.07;

    public static void main(String[] args) throws FileNotFoundException
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws FileNotFoundException
    {
        answer = new Label("Price: ");
        price = new Label("");
        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 = new ListView < > ();
        listView.setPrefSize(200, 170);
        listView.getItems().addAll(listArray);

        //list view items book
        listView2 = new ListView < > ();
        listView2.setPrefSize(200, 170);
        listView2.getItems();


        // create label to display the selection
        Label selectedNameLabel = new Label("Select a Book");
        Label price = answer;

        //Button for selection
        Button addButton = new Button("Add to Cart");
        addButton.setOnAction(new AddButtonListener());

        //Delete button
        Button removeButton = new Button("Remove Item");
        removeButton.setOnAction(new RemoveButtonListener());

        //Delete button
        Button clearButton = new Button("Clear All");
        clearButton.setOnAction(new ClearButtonListener());

        //Checkout
        Button checkoutButton = new Button("Check Out");
        checkoutButton.setOnAction(new CheckoutButtonListener());

        //Controls to HBox
        HBox hbox = new HBox(listView, listView2);

        //Controls to HBox2
        HBox hbox2 = new HBox(10, addButton, removeButton, clearButton, checkoutButton);
        hbox2.setAlignment(Pos.CENTER);

        //Controls to VBox
        VBox vbox = new VBox(10, hbox, selectedNameLabel,
                price, hbox2);
        vbox.setPadding(new Insets(10));
        vbox.setAlignment(Pos.CENTER);

        //Show
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    // Add button
    public class AddButtonListener implements EventHandler < ActionEvent >
    {
        @Override
        public void handle(ActionEvent e)
        {
            String value = listView.getSelectionModel().getSelectedItem();
            cartItems.add(value);
           // answer.setText("Price: " + Calc());
        }

    }
    // Subtract Button
    public class RemoveButtonListener implements EventHandler < ActionEvent >
    {

        @Override
        public void handle(ActionEvent e)
        {
            String value = listView.getSelectionModel().getSelectedItem();
            try {
                cartItems.remove(value);
                //answer.setText("Price: " + Calc());
            }
            catch (IllegalArgumentException ex) {
                //do nothing
            }
        }
    }
        //Clearbutton
        public class ClearButtonListener implements EventHandler < ActionEvent > 
    {

        @Override
        public void handle(ActionEvent e) 
        {
                    cartItems.removeAll();
                    answer.setText("Price: " + Calc());
        }
    }

        //Checkout
        public class CheckoutButtonListener implements EventHandler < ActionEvent > 
    {
        @Override
        public void handle(ActionEvent e) 
        {
                    answer.setText("Price: " + Calc());
        }
    }

    // Button Calculations
    private String Calc() {
        String line;
        double totalCost = 0.0, costOfItem = 0.0;
        File file = new File("BookPrices.txt");
        Scanner fileReader = null;
        try
        {
            fileReader = new Scanner(file);
        }
        catch (FileNotFoundException el)
        {
            el.printStackTrace();
        }

        while (fileReader.hasNextLine())
        {
            line = fileReader.nextLine();
            String[] cost = line.split(",");

            String title = cost[0];
            costOfItem = Double.parseDouble(cost[1]);

            for (int i = 0; i < cartItems.getItemCount(); i++)
            {
                if (title.equals(cartItems.getItem(i)))
                    totalCost += costOfItem;
            }
        }

        DecimalFormat myFormatter = new DecimalFormat("###.##");

        return myFormatter.format((salesTax * totalCost) + totalCost).toString();
    }
}
packappingcart1;
导入java.io.File;
导入java.io.FileNotFoundException;
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.stage.stage;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.VBox;
导入javafx.scene.layout.HBox;
导入javafx.geometry.Pos;
导入javafx.geometry.Insets;
导入javax.swing.*;
导入java.awt.List;
导入java.awt.event.ActionListener;
导入java.text.DecimalFormat;
导入java.util.Scanner;
公共类ShoppingCart1扩展了应用程序
{
自有品牌答案;
自有品牌价格;
列表视图列表视图;
ListView listView2;
私有字符串[]listArray=新字符串[7];
私有字符串[]listArray2=新字符串[7];
私有列表cartItems=新列表();
私人最终双重销售税=0.07;
公共静态void main(字符串[]args)引发FileNotFoundException
{
发射(args);
}
@凌驾
public void start(Stage primaryStage)引发FileNotFoundException
{
答案=新标签(“价格:”);
价格=新标签(“”);
弦线;
int指数=0;
File File=新文件(“BookPrices.txt”);
Scanner fileReader=新扫描仪(文件);
while(fileReader.hasNext())
{
line=fileReader.nextLine();
String[]titles=line.split(“,”);
listArray[index]=标题[0];
索引++;
}
//列表查看项目手册
listView=新建listView<>();
setPrefSize(200170);
listView.getItems().addAll(listArray);
//列表查看项目手册
listView2=新的ListView<>();
listView2.setPrefSize(200170);
listView2.getItems();
//创建标签以显示所选内容
标签selectedNameLabel=新标签(“选择一本书”);
标签价格=答案;
//选择按钮
Button addButton=新按钮(“添加到购物车”);
setOnAction(新的AddButtonListener());
//删除按钮
按钮移除按钮=新按钮(“移除项目”);
setOnAction(新的RemoveButtonListener());
//删除按钮
按钮清除按钮=新按钮(“全部清除”);
setOnAction(新的ClearButtonListener());
//结帐
按钮检查按钮=新按钮(“签出”);
setOnAction(新的checkExputtonListener());
//对HBox的控制
HBox HBox=新的HBox(列表视图,列表视图2);
//对HBox2的控制
HBox hbox2=新的HBox(10,添加按钮,移除按钮,清除按钮,检查按钮);
hbox2.设置校准(位置中心);
//控件到VBox
VBox VBox=新的VBox(10,hbox,selectedNameLabel,
价格,hbox2);
vbox.setPadding(新插图(10));
vbox.setAlignment(位置中心);
//展示
场景=新场景(vbox);
初级阶段。场景(场景);
primaryStage.show();
}
//添加按钮
公共类AddButtonListener实现EventHandler
{
@凌驾
公共无效句柄(ActionEvent e)
{
字符串值=listView.getSelectionModel().getSelectedItem();
增加(价值);
//response.setText(“价格:+Calc());
}
}
//减法按钮
公共类RemoveButtonListener实现EventHandler
{
@凌驾
公共无效句柄(ActionEvent e)
{
字符串值=listView.getSelectionModel().getSelectedItem();
试一试{
cartItems。删除(值);
//response.setText(“价格:+Calc());
}
捕获(IllegalArgumentException ex){
//无所事事
}
}
}
//清除按钮
公共类ClearButtonListener实现EventHandler
{
@凌驾
公共无效句柄(ActionEvent e)
{
cartItems.removeAll();
response.setText(“价格:+Calc());
}
}
//结帐
公共类CheckExputtonListener实现EventHandler
{
@凌驾
公共无效句柄(ActionEvent e)
{
response.setText(“价格:+Calc());
}
}
//按钮计算
私有字符串计算(){
弦线;
双重总成本=0.0,成本项目=0.0;
File File=新文件(“BookPrices.txt”);
Scanner fileReader=null;
尝试
{
fileReader=新扫描仪(文件);
}
catch(filenotfoundel异常)
{
el.printStackTrace();
}
while(fileReader.hasNextLine())
{
line=fileReader.nextLine();
字符串[]成本=行分割(“,”);
字符串标题=成本[0];
costOfItem=Double.parseDouble(成本[1]);
对于(int i=0;i