0

我正在尝试用字符串数组的值填充我的 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

2 回答 2

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);
  }
于 2012-11-19T20:39:40.030 回答
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);
}
于 2012-11-19T20:41:24.283 回答