• java 下拉框级联及相关(转)


    ActionLintsner都实现此接口,其它监听器可以监听的事件都可以被它捕获

    public interface ActionListener extends EventListener
    The listener interface for receiving action events.
    The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.

    actionPerformed(ActionEvent e)
              Invoked when an action occurs.

    ItemListener用于捕获带有item的组件产生的事件,如ComboBox,CheckBox,RadioButton,ToggleButton,接口中定义的itemStateChanged(ItemEvent e)将执行需要在已选定(或已取消选定)项时发生的操作 

     public interface ItemListenerextends EventListenerThe listener interface for receiving item events.
    The class that is interested in processing an item event implements this interface. The object created with that class is then registered with a component using the component's addItemListener method. When an item-selection event occurs, the listener object's itemStateChanged method is invoked.

    void itemStateChanged(ItemEvent e)
    Invoked when an item has been selected or deselected by the user. The code written for this method performs the operations that need to occur when an item is selected (or deselected).

    package swing.combox;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Rectangle;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class YearMonthDay extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private final int STARTYEAR = 2000;//年份的开始值
         private final int ENDYEAR = 2020;//年份的结束值
         
         JPanel contentPane;
         //年月日的选择框
         JComboBox<String> 
          cboYear = new JComboBox<String>(),
          cboMonth = new JComboBox<String>(),
          cboDay = new JComboBox<String>();
         
         //年月日标签
         JLabel jLabel1 = new JLabel();
         JLabel jLabel2 = new JLabel();
         JLabel jLabel3 = new JLabel();
         public YearMonthDay() {
          
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           jbInit();
          
         }
         private void jbInit(){
          
          contentPane = (JPanel) getContentPane();
          contentPane.setLayout(null);
          setSize(new Dimension(400, 300));
          setTitle("年月日下拉列表级联");
          // 年的下拉选择框
          cboYear.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
          cboYear.setBounds(new Rectangle(0, 0, 55, 18));
          // 月的下拉选择框
          cboMonth.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
          cboMonth.setBounds(new Rectangle(80, 0, 45, 18));
          cboMonth.addItemListener(new DateItemAdapter(this));
    //      cboMonth
          // 日的下拉选择框
          cboDay.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
          cboDay.setBounds(new Rectangle(150, 0, 45, 18));
          // cboDay.setEditable(true);
          // 年的label
          jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
          jLabel3.setText("年");
          jLabel3.setBounds(new Rectangle(60, 0, 20, 20));
          // 月的label
          jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
          jLabel2.setText("月");
          jLabel2.setBounds(new Rectangle(130, 0, 20, 20));
          // 日的label
          jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
          jLabel1.setText("日");
          jLabel1.setBounds(new Rectangle(200, 0, 20, 20));
          
          contentPane.add(cboYear);
          contentPane.add(cboMonth);
          contentPane.add(cboDay);
          contentPane.add(jLabel3);
          contentPane.add(jLabel2);
          contentPane.add(jLabel1);
          
          //添加初始值
          AddInfo();
         }
         
         private void AddInfo() {
          //年下拉选择框
          for (int i = STARTYEAR; i < ENDYEAR; i++) {
           cboYear.addItem("" + i);
          }
          
          //月下拉选择框
          for (int i = 0; i < 12; i++) {
           cboMonth.addItem("" + (i + 1));
          }
          
          //日下拉选择框
          for (int j = 0; j < 31; j++) {
           cboDay.addItem("" + (j + 1));
          }
         }
         public void cboMonth_itemStateChanged(ItemEvent e) {
          
          Object obj = cboMonth.getSelectedItem();// 取得选中月份
          
          if (obj != null) { 
           cboDay.removeAllItems();// 清空日的下拉列表框
           
           int month = Integer.valueOf(obj.toString());
           if (month==5) {//test  JComboBox api combox method setEnabled()
            this.cboDay.setEnabled(false);
        }
           int days = 31;
           if (month == 4 || month == 6 || month == 9 || month == 11) {
            days = 30;
           } else if (month == 2) {
             //取得选中年份
            int year = Integer.parseInt(cboYear.getSelectedItem()
              .toString());
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
             // 是闰年
             days = 29;
            } else {
             // 不是闰年
             days = 28;
            }
           }//if
           
           for (int j = 0; j < days; j++) {
            cboDay.addItem("" + (j + 1));
           }//for
          }//if
         }//if
         
        }//end class
    
    // 事件监听器
    class DateItemAdapter implements ItemListener {
     private YearMonthDay adaptee;
     DateItemAdapter(YearMonthDay adaptee) {
      this.adaptee = adaptee;
     }
     public void itemStateChanged(ItemEvent e) {
         if (e.getStateChange()==ItemEvent.SELECTED) {
             System.out.println("was invoked");
             adaptee.cboMonth_itemStateChanged(e);
        }
     }
    }
    package swing.combox;
    
    public class Test {
    
         public static void main(String[] args) {
             YearMonthDay testDate = new YearMonthDay();
             testDate.setVisible(true);
             testDate.setBounds(200, 200, 250, 100);
    
        }
    }

    http://blog.sina.com.cn/s/blog_5f20c4740100e3xk.html

    JComboBox ItemListener监听器

    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;

    import javax.swing.JComboBox;

    class itemListener implements ItemListener {

    public void itemStateChanged(ItemEvent e) {

       if (e.getStateChange() == ItemEvent.SELECTED) {
        JComboBox jcb = (JComboBox) e.getSource();

        System.out.println((String) (jcb.getSelectedItem()));
        System.out.println(jcb.getSelectedIndex());
       } else {

       }

    }

    }

           今天用到了JComboBox的事件监听类 ItemListener,程序在执行时发现事件总是被触发两次,上网看了些资料,原来和ItemListener类中的方法itemStateChanged()事件的itemState有关,itemState在这里的状态有两个,Selected 和 deSelected(即选中和未被选中),所以,当改变下拉列表中被选中的项的时候,其实是触发了两次事件,第一次是上次被选中的项的 State 由 Selected 变为 deSelected ,即取消选择, 第二次是本次被选中的项的 State 由 deSelected 变为 Selected ,即新选中,所以,必然的 ItemStateChanged 事件中的代码要被执行两次了。

    解决方法 就是:
      if(e.getStateChange() == ItemEvent.SELECTED){
      //要执行的代码
      }
      即为当事件是选中某一项的时候,才执行该代码。

    http://ribbonchen.blog.163.com/blog/static/118316505200988105942563/

    (总结)Swing组件的使用---下拉式菜单(menu),弹出式菜单(JPopupMenu),选项卡窗体(JTabbedPane)

    http://blog.csdn.net/lyxaiclr/article/details/7391318

     

     

     

     

  • 相关阅读:
    echarts折线图
    利用echarts制作词云
    本周总结
    本周总结
    云服务器项目数据库连接超时问题解决
    iOS下载图片失败
    解决后台json数据返回的字段需要替换的问题
    设置User Agent
    Xcode升级到9.3之后pod问题
    gitLab创建自己的私有库
  • 原文地址:https://www.cnblogs.com/softidea/p/4029975.html
Copyright © 2020-2023  润新知