2016-04-09 42 views
0

Kendi kutumu Formumda oluşturmak istiyorum. Bu kodu ben yapıyorum. Ama ComboBox hiçbir görünür (((Formda kendi öğelerini gösterme

public class GUI { 
    String RetVal1; 
    String[][] RetValArr; 
    private JFrame frame; 
    private JLabel lblNewLabel; 
    private JTextField Name_textField; 

    private JFormattedTextField Phone_formattedTextField; 


    static Connection conn3 = null; 
    static int EC1; 
    static int CurrentEntry; 
    static String CurrentEntrySTR; 

    private JTextField CurrentEnty_textField; 
    private JTable table; 

    public static void main(String[] args) { 

     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        GUI window = new GUI(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 
    public GUI() { 
     initialize(); 
    } 

    private void initialize() { 

     frame = new JFrame(); 
     frame.setBounds(100, 100, 504, 513); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JComboBox From_comboBox = new JComboBox(); 
     From_comboBox.setModel(new DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" })); 
     From_comboBox.setEditable(true); 
     From_comboBox.setMaximumRowCount(2); 
     From_comboBox.setBounds(274, 194, 186, 23); 
     frame.getContentPane().add(From_comboBox); 
     new ComboBoxDemo(frame); 



     JComboBox Status_comboBox = new JComboBox(); 
     Status_comboBox.setBounds(274, 281, 186, 23); 
     frame.getContentPane().add(Status_comboBox); 

     CurrentEnty_textField = new JTextField(); 
    } 
} 

Bu benim kendi ComboBox'ta ((

public class ComboBoxDemo { 

    private List<Country> countries; 
    private JComboBox cBox; 

    public ComboBoxDemo(JFrame frame) { 
     countries = createCountryList(); 
     cBox = createComboBox(countries); 
     frame.add(cBox); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private JComboBox createComboBox(List<Country> countries) { 
     final JComboBox comboBox = new JComboBox(countries.toArray()); 
     comboBox.setRenderer(new ComboBoxRenderer()); 
     comboBox.addItemListener(new ItemListener() { 
      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if (e.getStateChange() == ItemEvent.SELECTED) { 
        Country country = (Country) comboBox.getSelectedItem(); 
        System.out.println(country.getIso()); 
       } 
      } 
     }); 
     return comboBox; 
    } 

    private class ComboBoxRenderer extends DefaultListCellRenderer { 

     @Override 
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, 
       boolean cellHasFocus) { 
      JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
      Country country = (Country) value; 
      label.setText(country.getName()); 
      return label; 
     } 
    } 

    private List<Country> createCountryList() { 
     List<Country> list = new ArrayList<>(); 
     list.add(new Country("Afghanistan", "AF")); 
     list.add(new Country("Åland Islands", "AX")); 
     list.add(new Country("Albania", "AL")); 
     return list; 
    } 

    public class Country { 
     private String name; 
     private String iso; 

     public Country(String name, String iso) { 
      this.name = name; 
      this.iso = iso; 
     } 

     public String getName() { 
      return name; 
     } 

     public String getIso() { 
      return iso; 
     } 
    } 

} 

kodudur Ama benim GUI Formunda benim ComboBox gösteremez. Ben onu aramak gerekir formum ancak programı çalıştırdığımda hatalar yok, ancak Form'da bir combobox yok ...

cevap

0

Null mizanpajı kullanmak iyi bir fikir değil, uygulamanızı yeniden tasarlamalısınız. Bu tartışma: why-is-it-frowned-upon-to-use-a-null-layout-in-swing

Açılan kutunuz gösterilmez, çünkü boş düzende tüm bileşenlerin boyutunun ayarlanması gerekir. cBox için setBounds() kullanımını unutmuştunuz.