Javafx当按钮点击时,第二个列表视图上的文本会被新选择的文本替换,我如何让它添加它



我正在用Javafx制作一个基本的书店。我的问题是,当我选择一个书名,然后进入菜单单击"添加到购物车"时,购物车中已有的任何项目都会被替换。我有点纠结于如何修复它。

我遇到的另一个问题是添加我的Book类——基本上,它应该添加Book对象而不是String对象,每当我尝试这样做时,我都会遇到错误,因为我的Book类需要String和Double。我想,也许在我把绳子分开后,我会说上半场是一根绳子,下半场是双打,但这并没有真正奏效。我在下面尝试了一下,它确实有效,但看起来很奇怪,所以如果你能看看它,让我知道它是否正确,那会很有帮助!如有任何帮助,我们将不胜感激。这是书文本文件的布局书名,价格

//all imports here
public class GUI extends Application{
public static void main(String[] args)
{
launch(args);
}

public void start(Stage primaryStage)
{
final double WIDTH = 820.0, HEIGHT = 400.0;
ArrayList<String> list = new ArrayList<>();

BorderPane borderPane = new BorderPane();
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
Menu shoppingMenu = new Menu("Shopping");
MenuItem loadBooks = new MenuItem("Load Books");
MenuItem exitItem = new MenuItem("Exit");
fileMenu.getItems().addAll(loadBooks,exitItem);
MenuItem addSelected = new MenuItem("Add Selected Book");
MenuItem removeSelected = new MenuItem("Remove Selected Book");
MenuItem clearCart = new MenuItem("Clear Cart");
MenuItem checkOut = new MenuItem("Check Out");
shoppingMenu.getItems().addAll(addSelected, removeSelected, clearCart, checkOut);

menuBar.getMenus().addAll(fileMenu, shoppingMenu);
Label welcomeText = new Label("Welcome to the PFW Online Book Store!");
Label availableBooks = new Label("Avaliable Books");
Label shoopingCart = new Label("Shopping Cart");
VBox vbox = new VBox(10, welcomeText);
vbox.setAlignment(Pos.TOP_CENTER);
ListView<String> listView = new ListView<>();
listView.setPrefSize(400, 300);
ListView<String> listView2 = new ListView<>();
listView2.setPrefSize(400, 300);
GridPane grid = new GridPane();
grid.add(availableBooks, 0, 0);
grid.add(listView, 0, 1);
grid.add(shoopingCart, 1, 0);
grid.add(listView2, 1, 1);
grid.setHgap(20);
borderPane.setTop(menuBar);
borderPane.setCenter(vbox);
borderPane.setBottom(grid);
//event handlers
exitItem.setOnAction(event ->
{
primaryStage.close();
});

loadBooks.setOnAction(event2 ->{
FileChooser fileC = new FileChooser();
fileC.setTitle("Open");
File projectD = new File(System.getProperty("user.dir"));
if(projectD.isDirectory()) {
fileC.setInitialDirectory(projectD);
}

File selectedFile = fileC.showOpenDialog(primaryStage);


try {
Scanner scan = new Scanner(selectedFile);

while(scan.hasNext()) {
//puts the line into a string, then splits the string into an array
//then puts the first half of the array into an array list and then puts 
//into book object and then adds it to list
String bookT = scan.nextLine();
String book[] = bookT.split(",");

String title = book[0];
String priceS = book[1];
Double priceD = Double.parseDouble(priceS);

Book b = new Book(title,priceD);

list.add(b.toString());
}

} catch (FileNotFoundException e) {

e.printStackTrace();
}

listView.getItems().addAll(list);

});

//add selected book **This is where I'm having issues**
addSelected.setOnAction(event2 ->{

ObservableList<String> selections = listView.getSelectionModel().getSelectedItems();
//I think I need to use loop to allow more than one item to be added but not sure
listView2.getItems().setAll(selections);

});


Scene scene = new Scene(borderPane, WIDTH, HEIGHT);
primaryStage.setTitle("Book Store Shopping Cart");
primaryStage.setScene(scene);
primaryStage.show();
}

}
public class Book {
/*
* public String title;
• public double price;
• a constructor that accepts and assigns the title and price
• a toString() method that returns the title
*/
public String title;
public double price;

public Book(String title, double price) {
this.title = title;
this.price = price;
}

public String toString() {
return title;
}

}

关于您的第一个问题:

据我所知,您正在OberableList:上致电setAll()

listView2.getItems().setAll(selections);

这只是用参数中的项替换列表中的所有项。您需要使用addAll()将它们附加到列表中:

listView2.getItems().addAll(selections);

关于您的第二个问题:

您必须在不使用大写字母的情况下编写double,才能使用基元类型而不是包装类Double(此处详细介绍(。因此,初始化双值的行必须是

double priceD = Double.parseDouble(priceS);

最新更新