简体   繁体   English

如何从组合框中获取选定的字符串?

[英]How do I get the selected string from a combobox?

This is my main class.这是我的主课。 I'm trying to get the string that is being selected from the combobox and "get the wall color" from that string.我正在尝试从组合框中获取正在选择的字符串,并从该字符串中“获取墙壁颜色”。 The problem is that when I run the application, after hitting the playbutton I get an error saying that in line 75 - if (stringWallColor.equals("Default - Black")) , stringWallColor is null.问题是,当我运行应用程序时,在点击播放按钮后,我收到一条错误消息,指出在第 75 行 - if (stringWallColor.equals("Default - Black")) , stringWallColor 为空。

Does that I mean that it uses the public static String stringWallColor which is null as default?我的意思是它使用公共静态字符串 stringWallColor 默认为空吗? And how can I fix this?我该如何解决这个问题?

public static String stringWallColor;

    public static void main(String[] args) {
        launch(args);
        System.out.println("Done!");
    }

    public void start(Stage primaryStage) throws Exception {

        Main.primaryStage = primaryStage;

        MenuBar MENU = new MenuBar();
        MenuGenerator.menuCreator(MENU);
        Button playButton = new Button("Play Game");

        ComboBox<String> wallColorCombo = new ComboBox<>();
        wallColorCombo.setPromptText("Choose wall color");
        wallColorCombo.getItems().addAll(
                "Default - Black",
                "Dark Green",
                "Dark Red",
                "Dark Gray",
                "Saddle Brown",
                "Midnight Blue",
                "Dark Magenta",
                "Crimson",
                "Navy");
        stringWallColor = wallColorCombo.getSelectionModel().getSelectedItem();

        gameGrid = new GridPane();
        GridPane root = new GridPane();
        root.add(MENU,0,0);
        root.add(gameGrid, 0, 1);
        gameScene = new Scene(root,600,625);

        GridPane startGrid = new GridPane();
        startGrid.setHgap(20);
        startGrid.setVgap(20);
        playButton.setLineSpacing(10);
        startGrid.add(playButton,8,12);
        startGrid.add(wallColorCombo,7,12);
        GridPane root0= new GridPane();
        root0.add(startGrid,0,1);
        Scene startScene = new Scene(root0, 400, 400);

        primaryStage.setScene(startScene);
        primaryStage.setTitle(GameEngine.GAME_NAME);
        primaryStage.show();
        System.out.println("Default save file not loaded yet");

        playButton.setOnAction(e -> {
            MenuGenerator.loadDefaultSaveFile(primaryStage);
            System.out.println("Default save file loaded");
            primaryStage.setScene(gameScene); });
    }

    public static Color getWallColor() {
        Color wallColor = Color.BLACK;

        if (stringWallColor.equals("Default - Black"))
            wallColor = Color.BLACK;
        if (stringWallColor.equals("Dark Green"))
            wallColor = Color.DARKGREEN;
        if (stringWallColor.equals("Dark Red"))
            wallColor = Color.DARKRED;
        if (stringWallColor.equals("Dark Gray"))
            wallColor = Color.DARKGRAY;
        if (stringWallColor.equals("Saddle Brown"))
            wallColor = Color.SADDLEBROWN;
        if (stringWallColor.equals("Midnight Blue"))
            wallColor = Color.MIDNIGHTBLUE;
        if (stringWallColor.equals("Dark Magenta"))
            wallColor = Color.DARKMAGENTA;
        if (stringWallColor.equals("Crimson"))
            wallColor = Color.CRIMSON;
        if (stringWallColor.equals("Navy"))
            wallColor = Color.NAVY;

        return wallColor;
    }
}

Solved by setting stringWallColor = wallColorCombo.getSelectionModel().getSelectedItem();通过设置stringWallColor = wallColorCombo.getSelectionModel().getSelectedItem(); as an action for the playbutton.作为播放按钮的动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从comboBox中选择后,如何突出显示特定的Label? - How do I highlight specific Label when selected from the comboBox? 如何在组合框中获取选定的值? - How can I get selected value in combobox? 我如何从组合框中检查选定的值 - How I check selected value from combobox 如何获取 excel 中选定字符串的行、列坐标 - How do i get row,column coordinates on selected String in excel 如何使选定的“组合框”值显示不同的文本? - How do I make a selected Combobox value display different text? 如何从 JavaFX 中的 ComboBox 获取所选项目的数量? - How to get number of selected item from a ComboBox in JavaFX? 如何使用Selenium WebDriver(2)从dojo组合框中获取选定的值 - How to get the selected value from a dojo combobox using Selenium WebDriver(2) 如何从jtable内的组合框中获取值和选定的值? - How to get values and selected values from combobox inside jtable? 我从 string[] 动物中选择了一个随机元素,我想要线索等于所选元素。 我如何获得等于元素的线索? - I have a random element from the string[] animals selected and i want clues to equal the selected element. How do I get the clue to equal the element? 如何获得从JComboBox中选择的项目的索引? - How do I get the index of the item selected from a JComboBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM