2016-05-01 7 views
1

私はいくつかの助けが必要ですラジオボタンを押してリストにトッピングを追加し、次にピザのサイズをリストに追加すると、このピザオーダーシステムが作成されました。私が今必要とするのは、注文ボタンだけです。私はコードが正しいと思うが、何らかの理由でボタンがない。私が何か悪いことをしたかどうか確かめない。ボタンには「Place order」というラベルが付きます。ここで助け いただきありがとうございコードです: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 =新しいHBox(15.0、tomatoButton、pepperButton、mushroomButton、pizzaType、btn); ' –

答えて

0

は高速カタツムリに同意し、あなたはそれがhboxの中に表示されるようにするために、あなたのHBoxにボタンを追加する必要があります。指定した間隔ですべてのボタンを区切ります。

0

実際にボタンをHBoxに追加するには、ボタンをHBoxに追加する必要があります。

関連する問題