直接貼圖:
在寫國旗顯示代碼時忘記一點,每個節點只有一個父節點!之前就是當選中其中一項時,只有選中項有國旗,而下面選項中的國旗消失了,只有重新生成一次拷貝才行,原理弄清楚對寫代碼是很重要的。
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package regist;
- 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.geometry.VPos;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.ColumnConstraints;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.StackPane;
- import javafx.stage.Stage;
- import javafx.stage.StageStyle;
- import javafx.util.Callback;
- /**
- *
- * @author Administrator
- */
- public class Regist extends Application {
- public static final java.util.Map<String,ImageView> flagmap = new java.util.HashMap<String, ImageView>();
- public static String[] countries;
- public static Label[] country_label;
- //public static ;
- static{
- countries = new String[]{"cn","de","ir","jp"};
- for(String s : countries){
- final ImageView img = new ImageView(new Image(Regist.class.getResourceAsStream(s+".png")));
- flagmap.put(s, img);
- }
- country_label = new Label[]{
- new Label("中國",flagmap.get("cn")),
- new Label("德國",flagmap.get("de")),
- new Label("伊拉克",flagmap.get("ir")),
- new Label("日本",flagmap.get("jp"))
- };
- }
- private void showRegistDialog(){
- Stage registWin = new Stage();
- HBox hb = new HBox();
- GridPane pane = new GridPane();
- ColumnConstraints cons1 = new ColumnConstraints(140);
- ColumnConstraints cons2 = new ColumnConstraints(240);
- ColumnConstraints cons3 = new ColumnConstraints(40);
- pane.getColumnConstraints().addAll(cons1,cons2,cons3);
- HBox.setMargin(pane, new Insets(20,20,20,20));
- hb.getChildren().add(pane);
- //定義標籤
- Label l_name = new Label("姓名:");
- Label l_sex = new Label("性別:");
- Label l_country = new Label("國籍:");
- //放置標籤
- pane.add(l_name, 0, 0);
- l_name.setId("l_name");
- pane.add(l_sex, 0, 1);
- pane.add(l_country, 0, 2);
- //定義輸入框
- TextField t_name = new TextField("");
- t_name.setPromptText("輸入姓名");
- t_name.setPrefWidth(230);
- t_name.setPrefHeight(32);
- //定義性別項
- javafx.scene.control.ComboBox<String> c_sex = new ComboBox<String>();
- c_sex.getItems().addAll("男","女");
- c_sex.setPrefHeight(32);
- c_sex.setPrefWidth(120);
- //定義國籍
- final ComboBox<Label> c_country = new ComboBox<Label>();
- c_country.setId("country");
- c_country.getStyleClass().add("country");
- c_country.getItems().addAll(country_label);
- c_country.setPrefWidth(120);
- c_country.setPrefHeight(32);
- c_country.setCellFactory(new CountryFactory());
- c_country.getSelectionModel().selectFirst();
- final ImageView l_flag =Regist.flagmap.get("cn");
- final Label image = new Label("g",l_flag);
- c_country.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Label>(){
- @Override
- public void changed(ObservableValue<? extends Label> arg0, Label arg1, Label arg2) {
- System.out.println("單位個數:"+c_country.getItems().size());
- image.setGraphic(arg2.getGraphic());
- }
- });
- pane.add(t_name, 1, 0);
- pane.add(c_sex, 1, 1);
- pane.add(c_country, 1, 2);
- //pane.add(l_flag, 2, 2);
- image.setPrefHeight(32);
- image.setPrefWidth(32);
- pane.add(image , 2, 2);
- pane.setId("scene1");
- hb.setId("bg");
- Scene scene = new Scene(hb,490,120);
- registWin.setScene(scene);
- registWin.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage stage) {
- showRegistDialog();
- }
- }
- class CountryCell extends ListCell<Label>{
- public CountryCell(){
- }
- @Override
- public void updateItem(Label item,boolean empty){
- super.updateItem(item, empty);
- if(item==null){
- System.out.println("item爲空");
- return;
- }
- String str = item.getText();
- Label l_c = new Label();
- l_c.setText(str);
- System.out.println("國家:"+l_c.getText());
- if(str.equals("中國")){
- ImageView img = new ImageView(Regist.flagmap.get("cn").getImage());//重新生成的圖片,這個很重要
- l_c.setGraphic(img);
- }else if(str.equals("日本")){
- ImageView img = new ImageView(Regist.flagmap.get("jp").getImage());
- l_c.setGraphic(img);
- }else if(str.equals("德國")){
- ImageView img = new ImageView(Regist.flagmap.get("de").getImage());
- l_c.setGraphic(img);
- }else if(str.equals("伊拉克")){
- ImageView img = new ImageView(Regist.flagmap.get("ir").getImage());
- l_c.setGraphic(img);
- }
- this.setGraphic(l_c);
- }
- }
- class CountryFactory implements Callback<ListView<Label>,ListCell<Label>>{
- @Override
- public ListCell<Label> call(ListView<Label> listview) {
- return new regist.CountryCell();
- }
- }