我无法想象如何计算以下的小计,税金和总额。我试图在动作事件中完成我的计算。原因是,当从组合框中进行选择并添加到列表中时,我需要计算我的计算。然后,如果我取消选择一个项目,它将从小计和总计中减去。没有计算按钮。有人可以告诉我,如果我正确地思考这个问题。我尝试过很多不同的东西,但一直都会遇到错误。我真的没有任何例子可以解决,我真的很难想象这个。有人可以给我一个例子,指出我正确的方向吗?
double bcost[] = { 1.95, 1.50, 1.25, 2.95, 2.50, 1.50 };
double acost[] = { 5.95, 6.95, 8.95, 8.95, 10.95, 12.95, 6.95 };
double mcost[] = { 15.95, 13.95, 13.95, 11.95, 19.95, 20.95, 18.95, 13.95, 14.95 };
double dcost[] = { 5.95, 3.95, 5.95, 4.95, 5.95 };
String[] beverages = { "Soda", "Tea", "Coffee", "Mineral Water", "Juice", "Milk" };
String[] appetizers = { "Buffalo Wings", "Buffalo Fingers", "Potato Skins", "Nachos", "Mushroom Caps",
"Shrimp Cocktail" };
String[] maincourse = { "Seafood Alfredo", "Chicken Alfredo", "Chicken Picatta", "Turkey Club", "Lobster Pie",
"Prime Rib", "Shrimp Scampi", "Turkey Dinner", "Stuffed Chicken" };
String[] dessert = { "Apple Pie", "Sundae", "Carrot Cake", "Mud Pie", "Apple Crisp" };
//------------------------------------------------------------------------------
ComboBox<String> beverageComboBox = new ComboBox<String>();
ObservableList<String> items1 = FXCollections.observableArrayList(beverages);
beverageComboBox.getItems().addAll(items1);
Label bo = new Label("Beverage Ordered");
pane.add(bo, 2, 5);
ListView<String> list1 = new ListView<String>();
beverageComboBox.setOnKeyPressed((e) -> {
list1.getItems().add(beverageComboBox.getSelectionModel().getSelectedItem());
});
list1.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
list1.getItems().remove(list1.getSelectionModel().getSelectedIndex());
});
pane.add(list1, 3, 5);
GridPane.setRowSpan(list1, 5);
// --------------------------------------------------------------------------------------------------------------------
ComboBox<String> appetizerComboBox = new ComboBox<String>();
ObservableList<String> items2 = FXCollections.observableArrayList(appetizers);
appetizerComboBox.getItems().addAll(items2);
Label ao = new Label("Appetizer Ordered");
pane.add(ao, 2, 10);
ListView<String> list2 = new ListView<String>();
appetizerComboBox.setOnKeyPressed((e) -> {
list2.getItems().add(appetizerComboBox.getSelectionModel().getSelectedItem());
});
list2.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
list2.getItems().remove(list2.getSelectionModel().getSelectedIndex());
});
pane.add(list2, 3, 10);
GridPane.setRowSpan(list2, 5);
//-----------------------------------------------------------------------------
答案 0 :(得分:0)
制作一些复选框:
chkHockey = new CheckBox("Hockey");
chkBaseball = new CheckBox("Baseball");
chkFootball = new CheckBox("Football");
将它们声明为实例变量而不是局部变量。如果确实将它们声明为局部变量,则在列表中维护创建的复选框,或者在添加事件时将对象传递给处理函数。所以,你可以在处理函数中使用它们。
制作2个测试标签:
lbltotal = new Label("Sports chosen: 0");
lblChoices = new Label("None");
将click-method附加到所有复选框。如果将所有复选框放在List中,则可以使用循环来添加处理程序。
chkHockey.setOnAction(e -> handleButtonAction(e));
chkBaseball.setOnAction(e -> handleButtonAction(e));
chkFootball.setOnAction(e -> handleButtonAction(e));
处理程序功能:
private void handleButtonAction(ActionEvent e) {
int count = 0;
String choices = "";
if (chkHockey.isSelected()) {
count++;
choices += chkHockey.getText() + "\n";
}
if (chkBaseball.isSelected()) {
count++;
choices += chkBaseball.getText() + "\n";
}
if (chkFootball.isSelected()) {
count++;
choices += chkFootball.getText() + "\n";
}
lbltotal.setText("Sports chosen: " + count);
lblChoices.setText(choices);
}
尽管我给出的示例非常基本,但该方法可用于对事件进行任何计算方式。如果你可以计数,那么你可以进行其他计算,前提是你可以访问处理函数中的相关变量。
对于listview,您可以使用changeListener()执行相同的工作。在这里,勾选一个复选框时会发生计算;在listview的情况下,只要选择发生变化,就会进行计算。 Check out JavaFX listview
<强>更新强>
start()
方法中初始化这些对象。将更改侦听器设置为ListViews,如下所示:
list1.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends String> ov, String old_val, String new_val) -> {
changeHandler(); // This method will contain the calculations
});
其中一个ComboBox的更改侦听器示例:
beverageComboBox.valueProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
list1.getItems().add(beverageComboBox.getSelectionModel().getSelectedItem());
});
鉴于上面的代码片段,您可以填充ListViews。