英文:
Cannot get 2nd viewlist to take list as argument
问题
不能够得到第二个创建的视图列表来接收作为参数添加的“cartItems”值项目,无论我如何措辞或将什么内容放入列表视图中。尝试过 .getitems(cartItems)
但是无效。是否有其他语句或方式可以表达这个问题,以便通过添加按钮添加的项目可以移到第二个结账列表中。
package shoppingcart1;
import ...
public class ShoppingCart1 extends Application
{
private ...
@Override
public void start(Stage primaryStage) throws FileNotFoundException
{
...
// 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(); // 这里应该设置 listView2 的内容,类似于上面的 listView
...
}
public class AddButtonListener implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e)
{
String value = listView.getSelectionModel().getSelectedItem();
cartItems.add(value);
// answer.setText("Price: " + Calc());
}
}
...
}
注意:我只提供了代码的翻译部分,保留了原始结构和逻辑。在 listView2 部分,您需要添加类似于 listView2.getItems().addAll(...)
的语句,以将项目添加到第二个列表中。
英文:
can not get the second created view list to take in the "cartItems" value items added as argument no matter how I worded or what I place into the listview. Tried to .getitems(cartItems) but it would not take.
Is their any other statements or way i can word this so that idems added with the add button move to the second list for checkout
Full Code
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();
}
}
答案1
得分: 1
我认为您的代码主要问题在于ShoppingCart1
类的成员变量cartItems
是一个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(); i++)
{
if (title.equals(cartItems.get(i)))
totalCost += costOfItem;
}
}
DecimalFormat myFormatter = new DecimalFormat("###.##");
return myFormatter.format((salesTax * totalCost) + totalCost).toString();
}
}
英文:
I think the main problem with your code is that member cartItems
of class ShoppingCart1
is a java.awt.List
. You can't mix AWT and JavaFX components. In the code below, I changed that to be java.util.List
.
In the below code, I made the minimum amount of changes in order to make the buttons do what I think you want them to. I wouldn't say the below code constitutes a completed JavaFX application. My intention (and hope) is that it allows you to overcome the barrier you are currently stuck at.
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论