java - 未添加按钮

标签 java javafx radio-button

我需要一些帮助,我创建了这个披萨订购系统,当您按下单选按钮时,该系统将配料添加到列表中,然后将披萨的大小添加到列表中。我现在唯一需要的是订购按钮。我相信代码是正确的,但由于某种原因按钮不存在。不确定我是否做错了什么。该按钮标记为下订单。谢谢你的帮助 这是代码:

package loan;

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.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
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.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import loan.pizzas.OrderHandler;

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 {
        stage.setTitle("Pizza System");
        Button btn = new Button();
        btn.setText("place order");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
             public void handle(ActionEvent event) {
                 System.out.println("Order has been placed.");
                }
        });



        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);
    }
}

最佳答案

同意 Fast Snail 的观点,您需要将按钮添加到 HBox 中才能使其显示在 Hbox 中。为您提供以您指定的间距分隔的所有按钮。

关于java - 未添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963617/

相关文章:

java - 了解 Java 中的检查异常与非检查异常

java - asmack - 接收自定义 XML 消息 ||

javafx-2 - javafx2中的树项选择事件

java - 如何使用 java 实现类似 alt+tab 的功能?

javascript - 如何在不使用 id 的情况下在 javascript 中选择我的第二个单选按钮?

java - JAX-RS JSON 编码后缺少子对象

java - 错误类型错误: Cannot set property 'name' of undefined

JAVAFX 舞台会根据场景的变化而改变其大小。或者这就是我的想法

提交时的 JavaScript 单选按钮

java - Wicket:如何实现未分组的单选按钮