2012-11-19 77 views
0

我試圖填充我的JComboBox一個字符串數組的值,但仍低於空白是我的代碼無法填充的JComboBox

package UI; 

     import Class.*; 
    import java.awt.event.ActionEvent; 
     import java.awt.event.ActionListener; 
     import java.awt.event.ItemEvent; 
     import java.awt.event.ItemListener; 
     import java.io.*; 
     import java.util.Scanner; 
     import java.util.logging.Level; 
     import java.util.logging.Logger; 
      import javax.swing.ButtonGroup; 
      import javax.swing.JComboBox; 
       import javax.swing.JFileChooser; 
      import javax.swing.JOptionPane; 


     public class Smith_waterman extends javax.swing.JFrame { 


     private String[]algorithm_name = {"Needleman & Wunsch (global alignment)","Smith & Waterman (local alignment)"}; 
     private PairwiseAlignmentAlgorithm[] algorithm = {new NeedlemanWunsch(), 
    new SmithWaterman(), 
    }; 


/** 
* Creates new form main_menu 
*/ 
     public Smith_waterman() { 
    initComponents(); 
    algorithm_combo = new JComboBox(algorithm_name); 

有人可以告訴我該怎麼解決這個問題

+4

聖印蝙蝠俠! –

+0

@SamIam(我更喜歡Elliot Ness變體)我們可以在邏輯上縮進它。沒有..WAIT,那*完全是*他們期望的。或者說第三種方式 - 請使用一致的邏輯縮進代碼塊。 –

回答

2

看到這個例子,它會給你一些想法.................

public class MainClass extends JPanel implements ItemListener { 
    public MainClass() { 
     JComboBox jc = new JComboBox(); 
     jc.addItem("France"); 
     jc.addItem("Germany"); 
     jc.addItem("Italy"); 
     jc.addItem("Japan"); 
     jc.addItemListener(this); 
     add(jc); 
    } 

    public void itemStateChanged(ItemEvent ie) { 
     String s = (String)ie.getItem(); 
     System.out.println(s); 
    } 

    public static void main(String[] args) { 
    JFrame frame = new JFrame(); 
    frame.getContentPane().add(new MainClass()); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(200, 200); 
    frame.setVisible(true); 
    } 
2

要填充你需要使用的ComboBox中的addItem()方法的組合框

private String[] algorithm_name = {"Needleman & Wunsch (global alignment)","Smith & Waterman (local alignment)"}; 
JComboBox jc = new JComboBox(); 

// Then you can iterate over the array to populate the combobox 
for (String name : algorithm_name) { 
    jc.addItem(name); 
} 
+0

仍然不起作用 –

+0

奇怪的是,它應該工作。我再次檢查它,它在我的示例中起作用。 它仍然只是空白,或者你得到一個錯誤或什麼? – Altrim