所以这是我的Shoppinglist应用程序来启动窗口,不幸的是,产品部分的列表视图不会让任何交互发生在鼠标上,只有箭头键。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
public class ShoppingListApp extends Application {
private GroceryItem[] GroceryList;
ShopperListAppView1 view = new ShopperListAppView1();
private Shopper shopper = new Shopper();
private Carryable item ;
public ShoppingListApp() { GroceryList = Shopper.List1();}
public void start(Stage primaryStage){
Pane aPane = new Pane();
aPane.getChildren().add(view);
view.getButtonPane().getBuy().setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event) { handleBuyButtonPress();}
});
view.getButtonPane().getReturnb().setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) { handleReturnButtonPress();}
});
view.getButtonPane().getCheckout().setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) { handleCheckoutButtonPress();}
});
primaryStage.setTitle("Grocery Store Application");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(aPane));
view.update(GroceryList, 0);
primaryStage.show();
}
private void handleBuyButtonPress(){
for (int i = 0; i < Shopper.List1().length; i++){
if (view.getpList().getSelectionModel().getSelectedItem() == Shopper.List1()[i].getDescription()){
item = Shopper.List1()[i];
}
}
shopper.addItem(item);
view.update(GroceryList, 0);
}
private void handleReturnButtonPress(){
for(int i = 0; i < Shopper.List1().length; i++){
if (view.getcList().getSelectionModel().getSelectedItem() == Shopper.List1()[i].getDescription()){
item = Shopper.List1()[i];
}
}
shopper.removeItem(item);
view.update(GroceryList, 0);
}
private void handleCheckoutButtonPress(){
shopper.computeTotalCost();
shopper.packBags();
view.update(GroceryList, 0);
}
public static void main(String[] args) { launch(args); }
}
此后,是购物清单应用视图,显示控制器的窗口。这也保留了刷新屏幕的更新方法。
import javafx.geometry.Pos;
import javafx.scene.control.ListView;
import javafx.scene.control.Label;
import javafx.collections.FXCollections;
import javafx.scene.layout.Pane;
import javafx.scene.control.TextField;
public class ShopperListAppView1 extends Pane implements GroceryView {
private ListView<String> pList, sList, cList;
private ShopperButtonPane buttonPane;
private TextField priceText;
public ListView<String> getpList() { return pList; }
public ListView<String> getcList() { return cList; }
public ListView<String> getsList() { return sList; }
public ShopperButtonPane getButtonPane() { return buttonPane; }
public ShopperListAppView1(){
Label products = new Label("Product");
products.relocate(10, 10);
Label shoppingcart = new Label("Shopping Cart");
shoppingcart.relocate(220,10);
Label contents = new Label("Contents");
contents.relocate(430, 10);
Label price = new Label("Total Price:");
price.setStyle("-fx-font: 12 arial; -fx-base: WHITE; -fx-text-fill: rgb(0,0,0);");
price.setAlignment(Pos.TOP_LEFT);
price.relocate(565, 355);
price.setPrefSize(65,25);
priceText = new TextField();
priceText.relocate(630,355);
priceText.setPrefSize(100, 25);
priceText.setEditable(false);
pList = new ListView<String>();
pList.relocate(10, 45);
pList.setPrefSize(200, 300);
sList = new ListView<String>();
sList.relocate(220, 45);
sList.setPrefSize(200, 300);
cList = new ListView<String>();
cList.relocate(430, 45);
cList.setPrefSize(300, 300);
buttonPane = new ShopperButtonPane();
buttonPane.relocate(0, 0);
buttonPane.setPrefSize( 540,25);
getChildren().addAll(products, shoppingcart, contents, price, pList, cList, sList, buttonPane, priceText);
setPrefSize(740, 390);
}
public void update(GroceryItem[] model, int selectedItem) {
String[] Product = new String[Shopper.List1().length];
String[] Cart = new String[Shopper.List1().length];
String[] Contents = new String[Shopper.List1().length];
Shopper shopper = new Shopper();
for (int i = 0; i < Shopper.List1().length; i++) {
Product[i] = model[i].toString();
if (shopper.getCart()[i] != null){
Cart[i] = shopper.getCart()[i].getDescription();
}
}
buttonPane.getBuy().setDisable(pList.getSelectionModel().getSelectedIndex() < 0);
buttonPane.getReturnb().setDisable(cList.getSelectionModel().getSelectedIndex() < 0);
buttonPane.getCheckout().setDisable(cList.hasProperties());
priceText.setText(shopper.StringTotalCost());
priceText.setAlignment(Pos.BASELINE_RIGHT);
pList.setItems(FXCollections.observableArrayList(Product));
sList.setItems(FXCollections.observableArrayList(Cart));
}
}