我正在尝试使用字符串数组的值来填充我的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);
有人可以告诉我如何解决这个问题
答案 0 :(得分: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);
}
答案 1 :(得分: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);
}