Java Swing - 如何通過索引在JComboBox中設(shè)置所選項(xiàng)
我們想知道如何通過索引在JComboBox中設(shè)置所選項(xiàng)。
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JComboBox Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] selections = { "green", "red", "orange", "dark blue" };
JComboBox comboBox = new JComboBox(selections);
comboBox.setSelectedIndex(1);
System.out.println(comboBox.getSelectedItem());
frame.add(comboBox);
frame.pack();
frame.setVisible(true);
}
}