2016-05-01 79 views
1

我需要一些帮助我创建了这个比萨饼定购系统,当您按下单选按钮并将比萨饼的大小添加到列表中时,它将添加到列表中。我现在唯一需要的是订单按钮。我相信代码是正确的,但由于某种原因,按钮不在那里。不知道我做错了什么。该按钮被标记为放置顺序。谢谢大家帮忙 这里是代码:未添加java - 按钮

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

“我相信代码是正确的”行为是错误的,所以代码应该是错的。不要相信这样的事情.. – MikeCAT

+2

不应该按钮被“添加”到某个地方让它显示? – MikeCAT

+3

你不添加'btn' .right?添加它 'HBox topHBox = new HBox(15.0,tomatoButton,pepperButton,mushroomButton,pizzaType,btn);' –

回答

0

同意快速蜗牛,你需要将按钮添加到您的HBox中,以便它出现在横向盒。给你所有按你指定的间距分开的按钮。

0

如果您希望在HBox中进行应用,您实际上需要将按钮添加到您的HBox中。