【问题标题】:java - button not addedjava - 未添加按钮
【发布时间】:2016-08-26 02:34:15
【问题描述】:

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

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

【问题讨论】:

  • "我相信代码是对的" 行为是错误的,所以代码应该是错误的。不要相信这样的事情..
  • 不应该将按钮“添加”到某处以使其显示吗?
  • 你不加btn.对吗?加HBox topHBox = new HBox(15.0, tomatoButton, pepperButton, mushroomButton, pizzaType,btn);

标签: java javafx radio-button


【解决方案1】:

同意Fast Snail,您需要将按钮添加到您的HBox,以便它出现在Hbox中。为您提供所有按您指定的间距分隔的按钮。

【讨论】:

    【解决方案2】:

    如果你想让它在 HBox 中应用,你实际上需要将按钮添加到你的 HBox。

    【讨论】:

      猜你喜欢
      • 2012-05-01
      • 2015-06-29
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      相关资源
      最近更新 更多