将图像连接到选择框

时间:2019-11-19 22:45:50

标签: java javafx

你好,我是JavaFX的新手,所以我有一个带有动物列表的选择框,我想这样做,以便在选择框中有与答案之一相关的图像。例如,如果我有一只鹿的图像,则用户需要在选择框中选择鹿,如果他们选择了另外3只鹿,则会弹出错误的信息。这是我没有图像和选择框的代码,但是在连接它们时遇到了麻烦。

// launch the application 
public void start(Stage s) 
{ 
    // set title for the stage 
    s.setTitle("creating ChoiceBox"); 

    // create a button 
    Button b = new Button("show"); 

    // create a tile pane 
    TilePane r = new TilePane(); 

    // create a label 
    Label l = new Label("This is a choice box"); 
    Label l1 = new Label("nothing selected"); 

    // string array 
    String st[] = { "Arnab", "Andrew", "Ankit", "None" }; 

    // create a choiceBox 
    ChoiceBox c = new ChoiceBox(FXCollections.observableArrayList(st)); 

    // add a listener 
    c.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() { 

        // if the item of the list is changed 
        public void changed(ObservableValue ov, Number value, Number new_value) 
        { 

            // set the text for the label to the selected item 
            l1.setText(st[new_value.intValue()] + " selected"); 
        } 
    }); 

 //Add animal
       Image animimg = createImage("Images of Andrew");  
       ImageView imageView = new ImageView(animimg);





 vBox.setSpacing(20);   
       vBox.setMargin(c, new Insets(0, 0, canvasWidth, canvasHeight)); 
     //  vBox.setMargin(d, new Insets(10, 10, 5, 5)); 
       ObservableList list = vBox.getChildren(); 

       vBox.getChildren().add(imageView);
       list.addAll(c); 

       Scene scene = new Scene(vBox); 
       stage.setScene(scene);     
       stage.show(); 

} 

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

} 

0 个答案:

没有答案