import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class MovieTicketSystem extends Application { // 创建并初始化折扣对象 Discount[] discounts = { new StudentDiscount(), new ChildrenDiscount(), new VIPDiscount() }; @Override public void start(Stage primaryStage) throws Exception { // 设置窗口标题 primaryStage.setTitle("电影票销售系统"); // 创建Grid布局 GridPane gridPane = new GridPane(); gridPane.setAlignment(Pos.CENTER); gridPane.setHgap(10); gridPane.setVgap(10); // 创建UI控件 Label movieLabel = new Label("选择电影:"); ComboBox<String> movieComboBox = new ComboBox<>(); movieComboBox.getItems().addAll("功夫熊猫", "叶问", "疯狂动物城"); movieComboBox.setValue("功夫熊猫"); Label discountLabel = new Label("选择优惠方式:"); ComboBox<String> discountComboBox = new ComboBox<>(); discountComboBox.getItems().addAll("学生优惠", "儿童优惠", "VIP优惠"); discountComboBox.setValue("学生优惠"); Label priceLabel = new Label("原价:50元"); Label finalPriceLabel = new Label("最终价格:"); TextField finalPriceField = new TextField(); finalPriceField.setEditable(false); Button calculateButton = new Button("计算价格"); calculateButton.setOnAction(e -> { // 获取用户选择的电影和优惠方式 String movie = movieComboBox.getSelectionModel().getSelectedItem(); String discount = discountComboBox.getSelectionModel().getSelectedItem(); // 根据用户选择设置票价和折扣对象 MovieTicket mt = new MovieTicket(); if (movie.equals("功夫熊猫")) { mt.setPrice(60); } else if (movie.equals("叶问")) { mt.setPrice(70); } else if (movie.equals("疯狂动物城")) { mt.setPrice(80); } else { finalPriceField.setText("请选择正确的电影!"); return; } if (discount.equals("学生优惠")) { mt.setDiscount(discounts[0]); } else if (discount.equals("儿童优惠")) { mt.setDiscount(discounts[1]); } else if (discount.equals("VIP优惠")) { mt.setDiscount(discounts[2]); } double price = mt.getPrice(); finalPriceField.setText(price + "元"); }); // 添加UI控件到Grid布局中 gridPane.add(movieLabel, 0, 0); gridPane.add(movieComboBox, 1, 0); gridPane.add(discountLabel, 0, 1); gridPane.add(discountComboBox, 1, 1); gridPane.add(priceLabel, 0, 2); gridPane.add(finalPriceLabel, 0, 3); gridPane.add(finalPriceField, 1, 3); gridPane.add(calculateButton, 0, 4, 2, 1); // 创建场景并将Grid布局添加到场景中 Scene scene = new Scene(gridPane, 400, 250); primaryStage.setScene(scene); // 显示窗口 primaryStage.show(); } public static void main(String[] args) { launch(args); }把这段代码的电影票原价改成随选择的电影变化而变化
时间: 2024-02-14 17:17:57 浏览: 118
可以在calculateButton的事件监听器中根据用户选择的电影来设置原价。修改代码如下:
```
calculateButton.setOnAction(e -> {
// 获取用户选择的电影和优惠方式
String movie = movieComboBox.getSelectionModel().getSelectedItem();
String discount = discountComboBox.getSelectionModel().getSelectedItem();
// 根据用户选择设置票价和折扣对象
MovieTicket mt = new MovieTicket();
if (movie.equals("功夫熊猫")) {
mt.setPrice(60);
priceLabel.setText("原价:60元");
} else if (movie.equals("叶问")) {
mt.setPrice(70);
priceLabel.setText("原价:70元");
} else if (movie.equals("疯狂动物城")) {
mt.setPrice(80);
priceLabel.setText("原价:80元");
} else {
finalPriceField.setText("请选择正确的电影!");
return;
}
if (discount.equals("学生优惠")) {
mt.setDiscount(discounts[0]);
} else if (discount.equals("儿童优惠")) {
mt.setDiscount(discounts[1]);
} else if (discount.equals("VIP优惠")) {
mt.setDiscount(discounts[2]);
}
double price = mt.getPrice();
finalPriceField.setText(price + "元");
});
```
这样,每次用户选择不同的电影,原价标签都会自动更新。
阅读全文