java - 选择多个值

时间:2016-04-28 18:39:48

标签: java javafx scrollbar

我需要帮助。在我的代码中我实现了一个滚动条,但我如何选择多个值。例如,我如何同时选择像苹果和橙色。我不知道该怎么做。有人可以让我看看它是如何完成的。谢谢。

这是代码:

package lab;

    import javafx.application.Application;
    import javafx.collections.*;
    import javafx.event.*;
    import javafx.geometry.Pos;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.Color;
    import javafx.scene.control.*;
    import javafx.scene.Scene;
    import javafx.scene.shape.*;
    import javafx.scene.paint.*;

    public class pizza extends Application{
        private ComboBox<String> size;
        private ListView<String> toppings;
        private TextField order;
        private Button orderit, clearit;
        private Label lb_flavor, lb_tops, lb_order;
        private ObservableList<String> flavor =
                FXCollections.observableArrayList (
                        "Small", "Medium",
                        "Large", "extra Large");
        private ObservableList<String> tps =
                FXCollections.observableArrayList(
                        "pineapples", "pepperoni",
                        "peppers", "bacon", "sausage",
                        "ham");
        public void start(Stage primaryStage) {
            // areas to place the various components
            VBox pane = new VBox(15);
            HBox flavor_pane = new HBox(10);
            HBox topping_pane = new HBox(10);
            HBox order_pane = new HBox(10);

            lb_flavor = new Label("Sizes");
            size = new ComboBox(flavor);
            size.setVisibleRowCount(4);
            size.setValue(flavor.get(0)); // display the first one

            flavor_pane.getChildren().addAll(lb_flavor, size);

            lb_tops = new Label("toppings");
            toppings = new ListView(tps);
            toppings.setPrefSize(100,80);
    toppings.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);


            topping_pane.getChildren().addAll(lb_tops, toppings);

            lb_order = new Label("Order Summary");
            order = new TextField();
            order.setEditable(false);
            order.setPrefColumnCount(25);

            order_pane.getChildren().addAll(lb_order, order);

            orderit = new Button("Place Order");
            clearit = new Button("Clear Order");

            // Subscribe for when the user clicks the buttons
            OrderHandler oh = new OrderHandler();
            orderit.setOnAction(oh);
            clearit.setOnAction(oh);

            pane.getChildren().addAll(flavor_pane,
                    topping_pane, order_pane, orderit, clearit);

            Scene scene = new Scene(pane, 400, 300);
            primaryStage.setTitle("pizza ordering");
            primaryStage.setScene(scene);
            primaryStage.show();

        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Application.launch(args);
        }
    class OrderHandler implements EventHandler<ActionEvent>{
        public void handle(ActionEvent e) {
            // was it the clear button  
            if (e.getSource() == clearit) {
                order.setText("");
                toppings.getSelectionModel().clearSelection();
                size.setValue(flavor.get(0));
                return;
            }
            // flavor
            String result = size.getValue();
            // for toppings
            ObservableList<String> selections =
            toppings.getSelectionModel().getSelectedItems();
            // convert to an array
            Object[] objs = selections.toArray();
            for (int k =0 ; k < objs.length; k++){
                result += " " + objs[k].toString();
            }
            order.setText(result);
        }
    }
    }

这是一个披萨订购系统。当它运行时,你必须从滚动条中选择大小,然后从滚动条中选择浇头。然后一旦订单被按下。订购的内容将显示在订单摘要中。现在我只需要一次选择2个浇头但是你现在只能选择一个浇头。谢谢

0 个答案:

没有答案