程序添加JComboBox的item也能触发itemStateChanged事件吗

程序添加JComboBox的item也能触发itemStateChanged事件吗

直接看实例:

影音先锋电影 http://www.iskdy.com/

Java代码   收藏代码
  1. package share;  
  2.   
  3. import java.awt.EventQueue;  
  4. import java.awt.GridBagConstraints;  
  5. import java.awt.GridBagLayout;  
  6. import java.awt.Insets;  
  7. import java.awt.event.ItemEvent;  
  8. import java.awt.event.ItemListener;  
  9.   
  10. import javax.swing.JButton;  
  11. import javax.swing.JComboBox;  
  12. import javax.swing.JFrame;  
  13. import javax.swing.JPanel;  
  14. import javax.swing.border.EmptyBorder;  
  15.   
  16. import com.string.widget.util.ValueWidget;  
  17.   
  18. public class TestJComboBox extends JFrame {  
  19.   
  20.     private JPanel contentPane;  
  21.     private JComboBox<String> comboBox;  
  22.   
  23.     /** 
  24.      * Launch the application. 
  25.      */  
  26.     public static void main(String[] args) {  
  27.         EventQueue.invokeLater(new Runnable() {  
  28.             public void run() {  
  29.                 try {  
  30.                     TestJComboBox frame = new TestJComboBox();  
  31.                     frame.setVisible(true);  
  32.                 } catch (Exception e) {  
  33.                     e.printStackTrace();  
  34.                 }  
  35.             }  
  36.         });  
  37.     }  
  38.   
  39.     /** 
  40.      * Create the frame. 
  41.      */  
  42.     public TestJComboBox() {  
  43.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  44.         setBounds(100100450300);  
  45.         contentPane = new JPanel();  
  46.         contentPane.setBorder(new EmptyBorder(5555));  
  47.         setContentPane(contentPane);  
  48.         GridBagLayout gbl_contentPane = new GridBagLayout();  
  49.         gbl_contentPane.columnWidths = new int[]{000};  
  50.         gbl_contentPane.rowHeights = new int[]{00};  
  51.         gbl_contentPane.columnWeights = new double[]{1.00.0, Double.MIN_VALUE};  
  52.         gbl_contentPane.rowWeights = new double[]{0.0, Double.MIN_VALUE};  
  53.         contentPane.setLayout(gbl_contentPane);  
  54.           
  55.         comboBox = new JComboBox<String>();  
  56.         GridBagConstraints gbc_comboBox = new GridBagConstraints();  
  57.         gbc_comboBox.insets = new Insets(0005);  
  58.         gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;  
  59.         gbc_comboBox.gridx = 0;  
  60.         gbc_comboBox.gridy = 0;  
  61.         contentPane.add(comboBox, gbc_comboBox);  
  62.           
  63.         JButton btnAddItem = new JButton("add item");  
  64.         GridBagConstraints gbc_btnAddItem = new GridBagConstraints();  
  65.         gbc_btnAddItem.gridx = 1;  
  66.         gbc_btnAddItem.gridy = 0;  
  67.         contentPane.add(btnAddItem, gbc_btnAddItem);  
  68.           
  69.           
  70.         comboBox.addItemListener(new ItemListener()  
  71.         {  
  72.             @Override  
  73.             public void itemStateChanged(ItemEvent arg0)  
  74.             {  
  75.                 System.out.println("itemStateChanged");  
  76.             }  
  77.         });  
  78.         init222();  
  79.           
  80.     }  
  81.     private void init222(){  
  82.         comboBox.addItem("a");  
  83.         comboBox.addItem("b");  
  84.         comboBox.addItem("c");  
  85.     }  
  86.   
  87. }  

 

启动的时候,控制台打印:

itemStateChanged

说明启动之后,执行了itemStateChanged 方法,说明了触发了itemStateChanged 事件.

总结:程序添加addItem 也能触发itemStateChanged 事件

如何把init222 方法放在comboBox.addItemListener 前就不会触发itemStateChanged 事件

 

伦理片 http://www.dotdy.com/





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值