我正在尝试将书籍,CD或DVD中的三个项目之一存储到数组中,但我的添加方法无效。
这是GUI类。
/**
Class BookStoreApplication GUI represents a book store.
It gives the user the option to
- add a book to the store's inventory
- list all books in the store's inventory
Author: YOUR FULL NAME HERE
E-mail address: YOUR E-MAIL ADDRESS
Last changed: TODAY'S DATE
Lab 01
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class BookStoreGUI extends Application {
private String title;
private String author;
private double price;
private static int pages;
private static int playingTime;
private Label titleLabel;
private Label authorLabel;
private Label priceLabel;
private TextField titleTextField;
private TextField authorTextField;
private TextField priceTextField;
private GridPane inputGrid;
private RadioButton BookRadioButton;
private RadioButton CDRadioButton;
private RadioButton DVDRadioButton;
private VBox RadioVBox;
private ToggleGroup mediaGroup;
private Button addItemButton;
private Button displayInventoryButton;
private HBox buttonHBox;
private TextArea outputText;
private BookStoreHandler handler;
private Stage maessageWindow;
private GridPane topGrid;
private BorderPane mainPane;
private static int count = 0;
private static BookStoreItem[] Catalog;
public void start(Stage primaryStage) {
createInputComponents();
createOutputComponents();
createButtons();
createWindow();
Scene scene = new Scene(mainPane, 400, 500);
primaryStage.setTitle("Book Sotre");
primaryStage.setScene(scene);
primaryStage.show();
}
private void createInputComponents(){
titleLabel = new Label("Title");
authorLabel = new Label("Author's name");
priceLabel = new Label ("Price");
titleTextField = new TextField();
authorTextField = new TextField();
priceTextField = new TextField();
inputGrid = new GridPane();
inputGrid.setHgap(15);
inputGrid.setVgap(15);
inputGrid.add(titleLabel, 0 , 0);
inputGrid.add(titleTextField, 1, 0);
inputGrid.add(authorLabel, 0, 1);
inputGrid.add(authorTextField, 1, 1);
inputGrid.add(priceLabel, 0 , 2);
inputGrid.add(priceTextField, 1, 2);
CDRadioButton = new RadioButton("CD");
BookRadioButton = new RadioButton("Book");
DVDRadioButton = new RadioButton("DVD");
mediaGroup = new ToggleGroup();
CDRadioButton.setToggleGroup(mediaGroup);
BookRadioButton.setToggleGroup(mediaGroup);
DVDRadioButton.setToggleGroup(mediaGroup);
BookRadioButton.setSelected(true);
RadioVBox = new VBox(15);
RadioVBox.getChildren().add(CDRadioButton);
RadioVBox.getChildren().add(BookRadioButton);
RadioVBox.getChildren().add(DVDRadioButton);
topGrid = new GridPane();
topGrid.setHgap(20);
topGrid.setAlignment(Pos.CENTER);
topGrid.add(inputGrid, 0, 0);
topGrid.add(RadioVBox, 1, 0);
}
private void createOutputComponents () {
outputText = new TextArea();
outputText.setEditable(false);
}
private void createButtons() {
addItemButton = new Button("Add Item");
displayInventoryButton = new Button("Display Inventory");
addItemButton.setPrefSize(100, 50);
displayInventoryButton.setPrefSize(100, 50);
buttonHBox = new HBox(100);
buttonHBox.setAlignment(Pos.CENTER);
buttonHBox.getChildren().add(addItemButton);
buttonHBox.getChildren().add(displayInventoryButton);
handler = new BookStoreHandler();
addItemButton.setOnAction(handler);
displayInventoryButton.setOnAction(handler);
}
private void createWindow() {
mainPane = new BorderPane();
mainPane.setTop(topGrid);
mainPane.setCenter(outputText);
mainPane.setBottom(buttonHBox);
}
private class BookStoreHandler implements EventHandler<ActionEvent> {
public void handle(ActionEvent ae) {
BookStoreItem media;
if(ae.getSource () == addItemButton){
outputText.setText("");
title = titleTextField.getText().trim();
author = authorTextField.getText().trim();
price = Double.parseDouble(priceTextField.getText().trim());
if (CDRadioButton.isSelected()){
media = new CD(title, author, price, 0);
}
else if(DVDRadioButton.isSelected()) {
media = new DVD(title, author, price, 0);
}
else {
media = new Book(title, author, price, 0);
}
Catalog = Catalog.add(media);
}
}
}
}
这是我的错误消息。 BookStoreGUI.java:210:错误:找不到符号 Catalog = Catalog.add(media); ^ 符号:方法添加(BookStoreItem) location:变量BookStoreItem类型的目录 1错误
和她是来自add方法的目录类。
public class Catalog {
private BookStoreItem[] inventory;
private final int CAPACITY = 100;
private int count;
public Catalog() {
inventory = new BookStoreItem[CAPACITY];
count = 0;
}
public void add(BookStoreItem newItem) {
inventory[count] = newItem;
count++;
}
public boolean isAvailable(String title) {
boolean found = false;
for (int i = 0;i < count && !found;i++) {
if (title.equals(inventory[i].getTitle())) {
found = true;
}
}
return found;
}
public BookStoreItem getItem(String title) {
BookStoreItem desiredItem = null;
boolean found = false;
for (int i = 0;i < count && !found;i++) {
if (title.equals(inventory[i].getTitle())) {
desiredItem = inventory[i];
found = true;
}
}
return desiredItem;
}
public BookStoreItem[] getList() {
return inventory;
}
}
答案 0 :(得分:2)
首先,如果遵循Java命名约定,问题会更少 - 目前Catalog
是数组变量名和类型名。
其次,数组中 没有add
方法。您可能需要List
,例如ArrayList
。或者,您可能只需要Catalog
的实例,例如
// This replaces your private static BookStoreItem[] Catalog; declaration
Catalog catalog = new Catalog();
然后您可以在handle
方法中使用它:
catalog.add(media);
请注意,我已将其设为实例变量而非静态变量 - 说实话,我怀疑您是否需要任何静态变量。