无论我如何措辞或放置在列表视图中的内容如何,都无法获取第二个创建的视图列表以接受作为参数添加的“ cartItems”值项。尝试使用.getitems(cartItems),但不会。是他们的其他任何声明或方式吗?我可以这样说,以便使用“添加”按钮添加的思想移至第二个列表以进行结帐 完整代码
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();
}
}
问题来源:Stack Overflow
我认为您代码的主要问题是cartItems类的成员ShoppingCart1是java.awt.List。您不能混合使用AWT和JavaFX组件。在下面的代码中,我将其更改为java.util.List。
在下面的代码中,我进行了最少的更改,以使按钮能够执行我认为希望的操作。我不会说以下代码构成了完整的JavaFX应用程序。我的意图(也是希望)是,它可以帮助您克服目前遇到的障碍。
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<String> cartItems = new ArrayList<>();
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");
try (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();
listView2.getItems().add(value);
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);
listView2.getItems().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.clear(); //removeAll();
listView2.getItems().clear();
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.size() /*getItemCount()*/; i++)
{
if (title.equals(cartItems.get/*Item*/(i)))
totalCost += costOfItem;
}
}
DecimalFormat myFormatter = new DecimalFormat("###.##");
return myFormatter.format((salesTax * totalCost) + totalCost).toString();
}
}
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。