Java捕獲選項事件ItemEvent – JAVA編程語言程序開發技術文章

[java]
package com.han; 
 
import java.awt.Container; 
import java.awt.FlowLayout; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.util.Vector; 
 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
 
/**
 * 捕獲選項事件ItemEvent
 * @author HAN
 *
 */ 
public class ItemEvent_1 extends JFrame { 
 
    /**
     * 
     */ 
    private static final long serialVersionUID = -9005090453541883935L; 
 
    public ItemEvent_1() { 
        // TODO Auto-generated constructor stub  
        Container container = getContentPane(); 
        container.setLayout(new FlowLayout()); 
        Vector<String> vector = new Vector<String>(); 
        for (int i = 1; i<6; i++)  
            vector.add("選項" + i); 
        DefaultComboBoxModel<String> defaultComboBoxModel =  
                new DefaultComboBoxModel<String>(vector); 
        JComboBox<String> comboBox = new JComboBox<String>(defaultComboBoxModel); 
        container.add(comboBox); 
        comboBox.addItemListener(new ItemListener() { 
 
            @Override 
            public void itemStateChanged(ItemEvent e) { 
                // TODO Auto-generated method stub  
                String item = e.getItem().toString(); 
                int stateChange = e.getStateChange(); 
                if (stateChange == ItemEvent.SELECTED) { 
                    System.out.println("此次事件由選中“" + item + "”觸發!"); 
                }else if (stateChange == ItemEvent.DESELECTED) { 
                    System.out.println("此次事件由取消選中“" + item + "”觸發!"); 
                }else { 
                    System.out.println("此次事件由其它原因觸發!"); 
                } 
            } 
             
        }); 
    } 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub  
        ItemEvent_1 frame = new ItemEvent_1(); 
        frame.setTitle("選項事件示例"); 
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        frame.setBounds(0, 0, 300, 100); 
    } 
 

package com.han;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;

/**
 * 捕獲選項事件ItemEvent
 * @author HAN
 *
 */
public class ItemEvent_1 extends JFrame {

 /**
  *
  */
 private static final long serialVersionUID = -9005090453541883935L;

 public ItemEvent_1() {
  // TODO Auto-generated constructor stub
  Container container = getContentPane();
  container.setLayout(new FlowLayout());
  Vector<String> vector = new Vector<String>();
  for (int i = 1; i<6; i++)
   vector.add("選項" + i);
  DefaultComboBoxModel<String> defaultComboBoxModel =
    new DefaultComboBoxModel<String>(vector);
  JComboBox<String> comboBox = new JComboBox<String>(defaultComboBoxModel);
  container.add(comboBox);
  comboBox.addItemListener(new ItemListener() {

   @Override    www.aiwalls.com
   public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub
    String item = e.getItem().toString();
    int stateChange = e.getStateChange();
    if (stateChange == ItemEvent.SELECTED) {
     System.out.println("此次事件由選中“" + item + "”觸發!");
    }else if (stateChange == ItemEvent.DESELECTED) {
     System.out.println("此次事件由取消選中“" + item + "”觸發!");
    }else {
     System.out.println("此次事件由其它原因觸發!");
    }
   }
   
  });
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ItemEvent_1 frame = new ItemEvent_1();
  frame.setTitle("選項事件示例");
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.setBounds(0, 0, 300, 100);
 }

}

作者:Gaowen_HAN

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。