Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 297|回复: 0

[Java基础知识]自定义事件与监听器

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 03:51:25 | 显示全部楼层 |阅读模式
    程序运行图:



    //运行方法:java   chen.ItemChooser$Demo  one two three four fiev
    package chen;
    import java.awt.*;  
      
       
       
         
       

       
       
      

      import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import java.util.*;

    /**
      * This class is a Swing component that presents a choice to the user.  It
      * allows the choice to be presented in a JList, in a JComboBox, or with a
      * bordered group of JRadioButton components.  Additionally, it displays the
      * name of the choice with a JLabel.  It allows an arbitrary value to be
      * associated with each possible choice.  Note that this component only allows
      * one item to be selected at a time.  Multiple selections are not supported.
      **/
    public class ItemChooser extends JPanel {
         // These fields hold property values for this component
         String name;           // The overall name of the choice
         String[] labels;       // The text for each choice option
         Object[] values;       // Arbitrary values associated with each option
         int selection;         // The selected choice
         int presentation;      // How the choice is presented

         // These are the legal values for the presentation field
         public static final int LIST = 1;
         public static final int COMBOBOX = 2;
         public static final int RADIOBUTTONS = 3;

         // These components are used for each of the 3 possible presentations
         JList list;                     // One type of presentation
         JComboBox combobox;             // Another type of presentation
         JRadioButton[] radiobuttons;    // Yet another type

         // The list of objects that are interested in our state
         ArrayList listeners = new ArrayList();

         // The constructor method sets everything up
          public ItemChooser(String name, String[] labels, Object[] values,
                 int defaultSelection, int presentation)
          {
             this.name = name;
             this.labels = labels;
             this.values = values;
             this.selection = defaultSelection;
             this.presentation = presentation;

            // If no values were supplied, use the labels
            if (values == null) this.values = labels;

            // Now create content and event handlers based on presentation type
             switch(presentation) {
               case LIST: initList(); break;
               case COMBOBOX: initComboBox(); break;
               case RADIOBUTTONS: initRadioButtons(); break;
    }
          }

          // Initialization for JList presentation
          void initList() {
             list = new JList(labels);          // Create the list
             list.setSelectedIndex(selection);  // Set initial state

             // Handle state changes
             list.addListSelectionListener(new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent e) {
                         ItemChooser.this.select(list.getSelectedIndex());
                    }
                });

             // Lay out list and name label vertically
             this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // vertical
             this.add(new JLabel(name));        // Display choice name
             this.add(new JScrollPane(list));   // Add the JList
          }
          
         // Initialization for JComboBox presentation
         void initComboBox() {
             combobox = new JComboBox(labels);         // Create the combo box
             combobox.setSelectedIndex(selection);     // Set initial state

             // Handle changes to the state
             combobox.addItemListener(new ItemListener() {
                     public void itemStateChanged(ItemEvent e) {
                           ItemChooser.this.select(combobox.getSelectedIndex());
                     }
                 });

             // Lay out combo box and name label horizontally
             this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
             this.add(new JLabel(name));
             this.add(combobox);
        }
          
         // Initialization for JRadioButton presentation
         void initRadioButtons() {
              // Create an array of mutually exclusive radio buttons
             radiobuttons = new JRadioButton[labels.length];   // the array
             ButtonGroup radioButtonGroup = new ButtonGroup(); // used for exclusion
             ChangeListener listener = new ChangeListener() {  // A shared listener
                public void stateChanged(ChangeEvent e) {
                     JRadioButton b = (JRadioButton)e.getSource();
                        if (b.isSelected()) {
                            // If we received this event because a button was
                            // selected, then loop through the list of buttons to
                             // figure out the index of the selected one.
                             for(int i = 0; i < radiobuttons.length; i++) {
                                        if (radiobuttons == b) {
                                          ItemChooser.this.select(i);
                                           return;
                                        }
                              }
                          }
                   }
                };

            // Display the choice name in a border around the buttons
           this.setBorder(new TitledBorder(new EtchedBorder(), name));
           this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

              // Create the buttons, add them to the button group, and specify
              // the event listener for each one.
              for(int i = 0; i < labels.length; i++) {
                      radiobuttons = new JRadioButton(labels);
                      if (i == selection) radiobuttons.setSelected(true);
                               radiobuttons.addChangeListener(listener);
                               radioButtonGroup.add(radiobuttons);
                               this.add(radiobuttons);
              }
         }
          
         // These simple property accessor methods just return field values
         // These are read-only properties.  The values are set by the constructor
         // and may not be changed.
         public String getName() { return name; }
         public int getPresentation() { return presentation; }
         public String[] getLabels() { return labels; }
         public Object[] getValues() { return values; }
          
         /** Return the index of the selected item */
         public int getSelectedIndex() { return selection; }
          
         /** Return the object associated with the selected item */
         public Object getSelectedValue() { return values[selection]; }
          
         /**
          * Set the selected item by specifying its index.  Calling this
          * method changes the on-screen display but does not generate events.
          **/
         public void setSelectedIndex(int selection) {
             switch(presentation) {
               case LIST: list.setSelectedIndex(selection); break;
               case COMBOBOX: combobox.setSelectedIndex(selection); break;
               case RADIOBUTTONS: radiobuttons[selection].setSelected(true); break;
              }
              this.selection = selection;
         }

         /**
          * This internal method is called when the selection changes.  It stores
          * the new selected index, and fires events to any registered listeners.
          * The event listeners registered on the JList, JComboBox, or JRadioButtons
          * all call this method.
          **/
         protected void select(int selection) {
              this.selection = selection;  // Store the new selected index
              if (!listeners.isEmpty()) {  // If there are any listeners registered
                    // Create an event object to describe the selection
                    ItemChooser.Event e =new ItemChooser.Event(this, selection, values[selection]);
                    // Loop through the listeners using an Iterator
                    for(Iterator i = listeners.iterator(); i.hasNext();) {
                         ItemChooser.Listener l = (ItemChooser.Listener)i.next();
                         l.itemChosen(e);  // Notify each listener of the selection
                    }
              }
         }

         // These methods are for event listener registration and deregistration
         public void addItemChooserListener(ItemChooser.Listener l) {
              listeners.add(l);
         }
         public void removeItemChooserListener(ItemChooser.Listener l) {
              listeners.remove(l);
         }

         /**
          * This inner class defines the event type generated by ItemChooser objects
          * The inner class name is Event, so the full name is ItemChooser.Event
          **/
         public static class Event extends java.util.EventObject {
              int selectedIndex;      // index of the selected item
              Object selectedValue;   // the value associated with it
              public Event(ItemChooser source,int selectedIndex, Object selectedValue) {
                  super(source);
                  this.selectedIndex = selectedIndex;
                  this.selectedValue = selectedValue;
             }

             public ItemChooser getItemChooser() { return (ItemChooser)getSource();}
             public int getSelectedIndex() { return selectedIndex; }
             public Object getSelectedValue() { return selectedValue; }
         }

         /**
          * This inner interface must be implemented by any object that wants to be
          * notified when the current selection in a ItemChooser component changes.
          **/
         public interface Listener extends java.util.EventListener {
               public void itemChosen(ItemChooser.Event e);
         }

         /**
          * This inner class is a simple demonstration of the ItemChooser component
          * It uses command-line arguments as ItemChooser labels and values.
          **/
         public static class Demo {
              public static void main(String[] args) {
                  // Create a window, arrange to handle close requests
                  final JFrame frame = new JFrame("ItemChooser Demo");
                  frame.addWindowListener(new WindowAdapter() {
                     public void windowClosing(WindowEvent e) {System.exit(0);}
                  });

             // A "message line" to display results in
             final JLabel msgline = new JLabel(" ");

             // Create a panel holding three ItemChooser components
             JPanel chooserPanel = new JPanel();
             final ItemChooser c1 = new ItemChooser("Choice #1", args, null, 0,ItemChooser.LIST);
             final ItemChooser c2 = new ItemChooser("Choice #2", args, null, 0,ItemChooser.COMBOBOX);
             final ItemChooser c3 = new ItemChooser("Choice #3", args, null,  

    0,ItemChooser.RADIOBUTTONS);
         // An event listener that displays changes on the message line
                 ItemChooser.Listener l = new ItemChooser.Listener() {
                     public void itemChosen(ItemChooser.Event e) {
                          msgline.setText(e.getItemChooser().getName() + ": " +
                                             e.getSelectedIndex() + ": " +
                                             e.getSelectedValue());
                           }
                   };
                 c1.addItemChooserListener(l);
                 c2.addItemChooserListener(l);
                 c3.addItemChooserListener(l);

                 // Instead of tracking every change with a ItemChooser.Listener,
                 // applications can also just query the current state when
                 // they need it.  Here"s a button that does that.
                 JButton report = new JButton("Report");
                 report.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent e) {
                              // Note the use of multi-line italic HTML text
                              // with the JOptionPane message dialog box.
                             String msg = "<html><i>" +
                                c1.getName() + ": " + c1.getSelectedValue() + "<br>"+
                                c2.getName() + ": " + c2.getSelectedValue() + "<br>"+
                                c3.getName() + ": " + c3.getSelectedValue() + "</i>";
                             JOptionPane.showMessageDialog(frame, msg);
                             }
                   });

               // Add the 3 ItemChooser objects, and the Button to the panel
               chooserPanel.add(c1);
               chooserPanel.add(c2);
               chooserPanel.add(c3);
               chooserPanel.add(report);

                 // Add the panel and the message line to the window
                 Container contentPane = frame.getContentPane();
                 contentPane.add(chooserPanel, BorderLayout.CENTER);
                 contentPane.add(msgline, BorderLayout.SOUTH);
         // Set the window size and pop it up.
                 frame.pack();
                 frame.show();
           }
         }
    }


      

      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/1/035124609.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-1 23:34 , Processed in 0.461897 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表