Advertisement
Guest User

GUI3

a guest
Oct 20th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package gui.pizza;
  2.  
  3. import javafx.application.Application;
  4. import javafx.scene.Node;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.CheckBox;
  8. import javafx.scene.control.RadioButton;
  9. import javafx.scene.control.TextArea;
  10. import javafx.scene.control.ToggleGroup;
  11. import javafx.scene.layout.GridPane;
  12. import javafx.stage.Stage;
  13.  
  14. public final class Pizza extends Application {
  15.  
  16.     private Configuration config = new Configuration();
  17.  
  18.     private GridPane checkPane, radioPane;
  19.  
  20.     private TextArea summary;
  21.  
  22.     public void start(Stage primStage) {
  23.  
  24.         GridPane root = new GridPane();
  25.         checkPane = new GridPane();
  26.         radioPane = new GridPane();
  27.  
  28.         Scene scene = new Scene(root, 640, 480);
  29.         primStage.setScene(scene);
  30.         primStage.show();
  31.  
  32.         root.add(checkPane, 0, 0);
  33.         root.add(radioPane, 0, 1);
  34.  
  35.         String[] list = config.getToppingNames();
  36.         for (int a = 0, i = 0; i < list.length; i++) {
  37.             a = i % 2;
  38.             CheckBox tmpChk = new CheckBox(list[i]);
  39.             if (i < config.getNumberOfDefaultToppings()) {
  40.                 tmpChk.setDisable(true);
  41.                 tmpChk.setSelected(true);
  42.             }
  43.             checkPane.add(tmpChk, a, i - a);
  44.         }
  45.  
  46.         list = config.getSizeNames();
  47.         ToggleGroup tglGrp = new ToggleGroup();
  48.         for (int i = 0; i < list.length; i++) {
  49.             RadioButton tmpButton = new RadioButton((list[i]));
  50.             tmpButton.setToggleGroup(tglGrp);
  51.             radioPane.add(tmpButton, i, 0);
  52.         }
  53.  
  54.         Button orderButton = new Button("Bestellen");
  55.         orderButton.setOnAction(e -> order());
  56.  
  57.         summary = new TextArea("");
  58.         summary.setEditable(false);
  59.  
  60.         root.add(orderButton, 0, 2);
  61.         root.add(summary, 0, 3);
  62.  
  63.     }
  64.  
  65.     public static void main(String[] args) {
  66.         launch(args);
  67.     }
  68.  
  69.     private void order() {
  70.         boolean sizePressed = false;
  71.         for (Node radBtn : radioPane.getChildren()) {
  72.             sizePressed = (((RadioButton) radBtn).isSelected()) ? true : sizePressed;
  73.         }
  74.         if (sizePressed) {
  75.             StringBuffer str = new StringBuffer();
  76.             str.append("Sie haben eine Pizza bestellt!\nZutaten: ");
  77.             for (Node node : checkPane.getChildren()) {
  78.                 CheckBox check = (CheckBox) node;
  79.                 str.append((check.isSelected()) ? (check.getText() + ", ") : "");
  80.             }
  81.             str.setLength(str.length() - 1);
  82.             RadioButton activeButton = (RadioButton) ((RadioButton) radioPane.getChildren().get(0)).getToggleGroup().getSelectedToggle();
  83.             str.append("\nDie Größe ist: " + activeButton.getText());
  84.             str.append("\nVielen Dank.");
  85.             summary.setText(str.toString());
  86.         } else {
  87.             summary.setText("Bitte wählen Sie eine Pizzagröße");
  88.             return;
  89.         }
  90.  
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement