使用组合框更改面板

时间:2014-10-11 20:58:13

标签: javafx javafx-8

我想创建用于切换面板的组合框。例如,我想创建几个不同颜色的面板,并使用组合框我想只有一个可见:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class MainApp extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        final ComboBox comboBox = new ComboBox();
        comboBox.getItems().addAll(
            "Bar Chart",
            "Pie Chart");
        comboBox.setValue("Bar Chart");

        final Label label = new Label();

        final StackPane stack = new StackPane();

        Button btn = new Button();
        btn.setText("Read comboBox");
        btn.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                label.setText("selectd: " + comboBox.getValue());
                stack.getChildren();
            }
        });        

        stack.getChildren().add(0, new Rectangle(100,100,Color.BLUE));
        stack.getChildren().add(1, new Rectangle(100,100,Color.GREEN));

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(label, comboBox, btn);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}

你能帮我解决一下用于切换可见面板洞察力的组合框改变监听器的代码。

2 个答案:

答案 0 :(得分:1)

试试这个:

public class MainApp extends Application {
  @Override
  public void start(Stage primaryStage) {
    Rectangle blueRectangle = new Rectangle(100,100, Color.BLUE);
    Rectangle grinRectangle = new Rectangle(100,100, Color.GREEN);

    StackPane stackPane = new StackPane();
    stackPane.getChildren().add(0, blueRectangle);
    stackPane.getChildren().add(1, grinRectangle);

    ComboBox<String> comboBox = new ComboBox<>();
    comboBox.getItems().addAll("Blue rectangle", "Green rectangle");
    comboBox.setValue("Green rectangle");
    comboBox.setOnAction(e -> {
      switch (comboBox.getValue()) {
        case "Blue rectangle":
          blueRectangle.toFront();
          break;
        case "Green rectangle":
          grinRectangle.toFront();
          break;
      }
    });

    VBox vBox = new VBox();
    vBox.setAlignment(Pos.CENTER);
    vBox.setPadding(new Insets(5, 5, 5, 5));
    vBox.setSpacing(10);
    vBox.getChildren().addAll(comboBox, stackPane);

    Scene scene = new Scene(vBox, 300, 250);
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

答案 1 :(得分:1)

有很多方法可以实现这一点,您可以通过@Vitomir

尽快将ComboBox的值硬编码到开关案例中

这是另外一个示例,它考虑将矩形添加到StackPane中的顺序与将值添加到ComboBox的顺序相同,无论何时选择组合框(或选择并按下按钮),选中的矩形是可见的,其余的不是。

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ComboBoxIssue extends Application {
    @Override
    public void start(Stage primaryStage) {
        final ComboBox comboBox = new ComboBox();
        comboBox.getItems().addAll("Show Blue", "Show Green");

        final Label label = new Label();

        final StackPane stack = new StackPane();

        Button btn = new Button();
        btn.setText("Read comboBox");

        /**
         * Uncomment this to run on button selection
         * 
         */
        /*btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                setVisibility(stack, comboBox, label);
            }
        });*/


        comboBox.getSelectionModel().selectedIndexProperty()
                .addListener((ObservableValue<? extends Number> observable,
                            Number oldValue, Number newValue) ->
                        setVisibility(stack, comboBox, label)
        );

        stack.getChildren().add(new Rectangle(100, 100, Color.BLUE));
        stack.getChildren().add(new Rectangle(100, 100, Color.GREEN));

        // Placing it after adding rectangle to stack
        // will trigger the changelistener to show default rectangle
        comboBox.setValue("Show Blue");

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(label, comboBox, btn, stack);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public void setVisibility(Pane pane, ComboBox comboBox, Label label) {

        //Set Label
        label.setText("selectd: " + comboBox.getValue());

        // Make all children invisible
        for (Node node : pane.getChildren()) {
            node.setVisible(false);
        }
        // make the selected rectangle visible
        int selectedIndex = comboBox.getSelectionModel()
                .selectedIndexProperty().getValue();
        pane.getChildren().get(selectedIndex).setVisible(true);

    }

    public static void main(String[] args) {
        launch(args);
    }
}