JFX2中實現Combox顯示圖片

 直接貼圖:

  在寫國旗顯示代碼時忘記一點,每個節點只有一個父節點!之前就是當選中其中一項時,只有選中項有國旗,而下面選項中的國旗消失了,只有重新生成一次拷貝才行,原理弄清楚對寫代碼是很重要的。

 

  1. /* 
  2.  * To change this template, choose Tools | Templates 
  3.  * and open the template in the editor. 
  4.  */ 
  5. package regist; 
  6.  
  7. import javafx.application.Application; 
  8. import javafx.beans.value.ChangeListener; 
  9. import javafx.beans.value.ObservableValue; 
  10. import javafx.event.ActionEvent; 
  11. import javafx.event.EventHandler; 
  12. import javafx.geometry.Insets; 
  13. import javafx.geometry.VPos; 
  14. import javafx.scene.Scene; 
  15. import javafx.scene.control.*; 
  16. import javafx.scene.image.Image; 
  17. import javafx.scene.image.ImageView; 
  18. import javafx.scene.layout.ColumnConstraints; 
  19. import javafx.scene.layout.GridPane; 
  20. import javafx.scene.layout.HBox; 
  21. import javafx.scene.layout.StackPane; 
  22. import javafx.stage.Stage; 
  23. import javafx.stage.StageStyle; 
  24. import javafx.util.Callback; 
  25.  
  26. /** 
  27.  * 
  28.  * @author Administrator 
  29.  */ 
  30. public class Regist extends Application { 
  31.  
  32.     public static final java.util.Map<String,ImageView> flagmap = new java.util.HashMap<String, ImageView>(); 
  33.     public static  String[] countries; 
  34.     public static Label[] country_label; 
  35.     //public static ; 
  36.     static
  37.         countries = new String[]{"cn","de","ir","jp"}; 
  38.         for(String s : countries){ 
  39.             final ImageView img = new ImageView(new Image(Regist.class.getResourceAsStream(s+".png"))); 
  40.             flagmap.put(s, img); 
  41.         } 
  42.         country_label = new Label[]{ 
  43.             new Label("中國",flagmap.get("cn")), 
  44.             new Label("德國",flagmap.get("de")), 
  45.             new Label("伊拉克",flagmap.get("ir")), 
  46.             new Label("日本",flagmap.get("jp")) 
  47.         }; 
  48.     } 
  49.     private void showRegistDialog(){ 
  50.         Stage registWin = new Stage(); 
  51.         HBox hb = new HBox(); 
  52.         GridPane pane = new GridPane(); 
  53.         ColumnConstraints cons1 = new ColumnConstraints(140); 
  54.         ColumnConstraints cons2 = new ColumnConstraints(240); 
  55.         ColumnConstraints cons3 = new ColumnConstraints(40); 
  56.         pane.getColumnConstraints().addAll(cons1,cons2,cons3); 
  57.         HBox.setMargin(pane, new Insets(20,20,20,20)); 
  58.         hb.getChildren().add(pane); 
  59.         //定義標籤 
  60.         Label l_name = new Label("姓名:"); 
  61.         Label l_sex = new Label("性別:"); 
  62.         Label l_country = new Label("國籍:"); 
  63.         //放置標籤 
  64.         pane.add(l_name, 00); 
  65.         l_name.setId("l_name"); 
  66.         pane.add(l_sex, 01); 
  67.         pane.add(l_country, 02); 
  68.         //定義輸入框 
  69.         TextField t_name = new TextField(""); 
  70.         t_name.setPromptText("輸入姓名"); 
  71.         t_name.setPrefWidth(230); 
  72.         t_name.setPrefHeight(32); 
  73.         //定義性別項 
  74.         javafx.scene.control.ComboBox<String> c_sex = new ComboBox<String>(); 
  75.         c_sex.getItems().addAll("男","女"); 
  76.         c_sex.setPrefHeight(32); 
  77.         c_sex.setPrefWidth(120); 
  78.         //定義國籍 
  79.         final ComboBox<Label> c_country = new ComboBox<Label>(); 
  80.         c_country.setId("country"); 
  81.         c_country.getStyleClass().add("country"); 
  82.         c_country.getItems().addAll(country_label); 
  83.         c_country.setPrefWidth(120); 
  84.         c_country.setPrefHeight(32); 
  85.         c_country.setCellFactory(new CountryFactory()); 
  86.         c_country.getSelectionModel().selectFirst();         
  87.         final ImageView l_flag =Regist.flagmap.get("cn"); 
  88.         final Label image = new Label("g",l_flag); 
  89.         c_country.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Label>(){ 
  90.             @Override 
  91.             public void changed(ObservableValue<? extends Label> arg0, Label arg1, Label arg2) { 
  92.                 System.out.println("單位個數:"+c_country.getItems().size()); 
  93.                 image.setGraphic(arg2.getGraphic()); 
  94.             } 
  95.         }); 
  96.         pane.add(t_name, 10); 
  97.         pane.add(c_sex, 11); 
  98.         pane.add(c_country, 12); 
  99.         //pane.add(l_flag, 2, 2); 
  100.         image.setPrefHeight(32); 
  101.         image.setPrefWidth(32); 
  102.         pane.add(image , 22); 
  103.         pane.setId("scene1"); 
  104.         hb.setId("bg"); 
  105.         Scene scene = new Scene(hb,490,120); 
  106.         registWin.setScene(scene); 
  107.         registWin.show(); 
  108.     } 
  109.     public static void main(String[] args) { 
  110.         launch(args); 
  111.     } 
  112.      
  113.     @Override 
  114.     public void start(Stage stage) { 
  115.         showRegistDialog(); 
  116.     } 
  117. class CountryCell extends ListCell<Label>{ 
  118.     public CountryCell(){ 
  119.     } 
  120.     @Override 
  121.     public void updateItem(Label item,boolean empty){ 
  122.         super.updateItem(item, empty); 
  123.         if(item==null){ 
  124.             System.out.println("item爲空"); 
  125.             return
  126.         } 
  127.         String str = item.getText(); 
  128.         Label l_c = new Label(); 
  129.         l_c.setText(str); 
  130.         System.out.println("國家:"+l_c.getText()); 
  131.         if(str.equals("中國")){ 
  132.             ImageView img = new ImageView(Regist.flagmap.get("cn").getImage());//重新生成的圖片,這個很重要 
  133.             l_c.setGraphic(img); 
  134.         }else if(str.equals("日本")){ 
  135.             ImageView img = new ImageView(Regist.flagmap.get("jp").getImage()); 
  136.             l_c.setGraphic(img); 
  137.         }else if(str.equals("德國")){ 
  138.             ImageView img = new ImageView(Regist.flagmap.get("de").getImage()); 
  139.             l_c.setGraphic(img); 
  140.         }else if(str.equals("伊拉克")){ 
  141.             ImageView img = new ImageView(Regist.flagmap.get("ir").getImage()); 
  142.             l_c.setGraphic(img); 
  143.         } 
  144.         this.setGraphic(l_c); 
  145.     } 
  146. class CountryFactory implements Callback<ListView<Label>,ListCell<Label>>{ 
  147.     @Override 
  148.     public ListCell<Label> call(ListView<Label> listview) { 
  149.         return new regist.CountryCell(); 
  150.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章