日期:2014-05-20 浏览次数:20715 次
import java.awt.BorderLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; public class JComboboxTest extends javax.swing.JFrame { private JPanel jPanel1; private JComboBox jComboBox1; private String[] zh = new String[] { "中国", "美国" }; private String[] en = new String[] { "China", "USA" }; private ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(zh); public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JComboboxTest inst = new JComboboxTest(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public JComboboxTest() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { jPanel1 = new JPanel(); getContentPane().add(jPanel1, BorderLayout.CENTER); jComboBox1 = new JComboBox(); jPanel1.add(jComboBox1); jComboBox1.setModel(jComboBox1Model); jComboBox1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent evt) { jComboBox1ItemStateChanged(evt); } }); } pack(); setSize(400, 300); } catch (Exception e) { e.printStackTrace(); } } private void jComboBox1ItemStateChanged(ItemEvent evt) { if(evt.getStateChange()==1) JOptionPane.showMessageDialog(null, "你选择的是:"+evt.getItem()+" "+en[this.jComboBox1.getSelectedIndex()]); } }