繁体   English   中英

在JAVA FX中使用事件处理程序

[英]Using event handler in JAVA FX

在我们的课程中,我们得到了一个作业,我们应该创建一个比萨饼GUI,使用顶部的复选框和用于比萨饼大小和类型的单选按钮。

我们已经为GUI创建了基础,甚至实现了逻辑,但遇到了一个小问题。

在我的程序中,我希望用户选择其浇头,比萨饼大小和比萨饼类型。 用户完成上述任务后,我希望他们单击​​过程选择,然后将信息以及销售价格添加到新的文本区域中。

不幸的是,即使在新的textarea中调用字符串(我存放所有内容的字符串)时,我仍然收到空白。

因此,我相信我没有适当地要求处理程序中的操作。 我也收到警告“未使用事件参数”

我在下面剪切了我的代码的一部分,如您所见,我试图将所有数据存储在ordertext中,然后在新的文本区域orderscreen中调用它。 我希望有人能发现我犯的错误,或者让我对我所忽略的东西有所了解。 谢谢

    TextArea orderscreen = new TextArea();
    orderscreen.setPrefColumnCount(50);
    orderscreen.setPrefRowCount(7);    
    grid.add(orderscreen, 0, 4);
    orderscreen.setText(ordertext);

          btn.setOnAction((ActionEvent event) -> {
              String mytoppings = "";
              double mytopcost = 0.0;

              if (chkTom.isSelected()) {
                  mytoppings = mytoppings + "Tomato "; // Topping
                  mytopcost += 1.50; // price
              }

              if (chkGP.isSelected()) {
                  mytoppings = mytoppings + "Green Peppers "; // Topping
                  mytopcost += 1.50; // pice
              }

              if (chkBO.isSelected()) {
                  mytoppings = mytoppings + "Black Olives "; // Topping
                  mytopcost += 1.50; // pice
              }

              if (chkMR.isSelected()) {
                  mytoppings = mytoppings + "MushRooms "; // Topping
                  mytopcost += 1.50; // pice
              }

              if (chkEC.isSelected()) {
                  mytoppings = mytoppings + "Extra Cheese "; // Topping
                  mytopcost += 1.50; // pice
              }

              if (chkPep.isSelected()) {
                  mytoppings = mytoppings + "Peppeoni "; // Topping
                  mytopcost += 1.50; // pice
              }

              if (chkSS.isSelected()) {
                  mytoppings = mytoppings + "Sausage "; // Topping
                  mytopcost += 1.50; // pice

              }
              else {
                  mytoppings = mytoppings + "No toppings selected ";
              }


              //Pizza Types

              String mypizzatype = "";
              // rbTC.setOnAction(e -> {
              if (rbTC.isSelected()) {
                  mypizzatype = mypizzatype + "Thin Crust "; // Type
              }
              // });

              //rbMC.setOnAction(e -> {
              if (rbMC.isSelected()) {
                  mypizzatype = mypizzatype + "Medium Crust "; // Type
              }
              // });

              if (rbP.isSelected()) {
                  mypizzatype = mypizzatype + "Pan "; // Type
              }

              // PIZZA SIZES
              String mypizzasize = "";
              Double smpzcost = 6.50;
              Double mdpzcost = 8.50;
              Double lgpzcost = 10.00;

              if (rbSM.isSelected()) {
                  mypizzatype = mypizzasize + "Small "; // Type
                  order = smpzcost;
              }

              if (rbMD.isSelected()) {
                  mypizzatype = mypizzasize + "Medium "; // Type
                  order = mdpzcost;
              }

              if (rbLG.isSelected()) {
                  mypizzatype = mypizzasize + "Large "; // Type
                  order = lgpzcost;
              }

              ordertext =  ("Your Order: "
                      + "\nPizza type: " + mypizzatype
                      + "\nPizza Size: " + mypizzasize
                      + "\nToppings: " + mytoppings
                      + "\nAmount Due: " + (order + mytopcost));
              System.out.println("Order Processed");
              //orderscreen.clear(); // WILL CLEAR
    });

我只是解决了问题

orderscreen.setText(ordertext);

需要更换

ordertext =  ("Your Order: "
     + "\nPizza type: " + mypizzatype
     + "\nPizza Size: " + mypizzasize
     + "\nToppings: " + mytoppings
     + "\nAmount Due: " + (order + mytopcost));
orderscreen.setText(ordertext);

我也需要改变

mypizzatype = mypizzasizemypizzasize= mypizzasize

无需事件处理程序。 在javafx中,您可以使用绑定。

我创建了一个简单的演示,演示了如何将gui控件( RadioButtonsChoiceBox )绑定到模型类中的属性并将其绑定到TextArea

package demo;

import javafx.application.Application;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

class User {
    private StringProperty order = new SimpleStringProperty();

    public String getOrder() {
        return order.get();
    }

    public void setOrder(String order) {
        this.order.set(order);
    }

    public StringProperty orderProperty() {
        return order;
    }
}

public class Demo extends Application {

    private User user = new User();

    @Override
    public void start(Stage stage) throws Exception {

        RadioButton tomatoButton = new RadioButton("Tomato");
        RadioButton pepperButton = new RadioButton("Pepper");
        RadioButton mushroomButton = new RadioButton("Mushrooms");

        ChoiceBox<String> pizzaType = new ChoiceBox<String>();
        pizzaType.getItems().addAll("", "Small", "Medium", "Large");
        pizzaType.getSelectionModel().selectFirst();

        HBox topHBox = new HBox(15.0, tomatoButton, pepperButton, mushroomButton, pizzaType);

        // create custom Binding that binds selection of radio buttons and choice box
        StringBinding orderBinding = createOrderBinding(tomatoButton.selectedProperty(), pepperButton.selectedProperty(), mushroomButton.selectedProperty(), pizzaType.getSelectionModel().selectedItemProperty());
        // bind orderBinding to orderProperty of User
        user.orderProperty().bind(orderBinding);

        TextArea orderArea = new TextArea();
        // bind orderProperty of User to textProperty of TextArea
        orderArea.textProperty().bindBidirectional(user.orderProperty());

        BorderPane root = new BorderPane();
        root.setTop(topHBox);
        root.setCenter(orderArea);

        Scene scene = new Scene(root, 400, 300);
        stage.setScene(scene);
        stage.show();
    }

    /**
     * Creates StringBinding between 4 provided arguments. Binding means that when value of one bound property is changed the whole binding is recomputed in computeValue method.
     * The value of computeValue is bound to User.orderProperty 
     */
    public StringBinding createOrderBinding(BooleanProperty tomato, BooleanProperty pepper, BooleanProperty mushroom, ReadOnlyObjectProperty<String> selectedPizzaType) {
        StringBinding binding = new StringBinding() {
            {
                // bind 4 provided properties.
                super.bind(tomato, pepper, mushroom, selectedPizzaType);
            }

            /* 
             * Fires each time bound property is modified. 
             */
            @Override
            protected String computeValue() {
                StringBuilder sb = new StringBuilder("Pizza content:\n");

                if (tomato.get())
                    sb.append("\tTomato\n");
                if (pepper.get())
                    sb.append("\tPepper\n");
                if (mushroom.get())
                    sb.append("\tMushroom\n");

                sb.append("Pizza type:\n").append("\t" + selectedPizzaType.get());
                return sb.toString();
            }
        };
        return binding;
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM