在這種情況下通常的做法是讓狗,貓&鳥在一個組合和另一個組合。對於根據第一個組合中的選擇填充的子類型。例如。
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
class PetSelector {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new GridLayout(0,1,5,5));
String[] speciesName = { "Dog", "Cat", "Bird"};
final String[][] breedName = {
{"Basenji", "Pomeranian", "Alsation"},
{"Burmese", "Persian", "Siamese"},
{"DoDo", "Archeopteryx", "Pheonix"}
};
final JComboBox petSpecies = new JComboBox(speciesName);
final JComboBox petBreed = new JComboBox();
ItemListener speciesListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
int ii = petSpecies.getSelectedIndex();
ComboBoxModel cbm = new DefaultComboBoxModel(breedName[ii]);
petBreed.setModel(cbm);
petBreed.requestFocusInWindow();
}
};
petSpecies.addItemListener(speciesListener);
gui.add(petSpecies);
gui.add(petBreed);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
啊謝謝@Andrew湯普森我忘了吊牌 – 2013-05-04 03:45:57
的一點是,用戶不會想,除非它再通過14元素長的名單解析。 – 2013-05-04 03:47:37
*「用戶不想通過14個元素的長列表進行分析」*我不同意,但是... ...您比我更瞭解您的用戶。 – 2013-05-04 03:49:31