2016-05-01 88 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中。