书上写的关于监听方面的内容不是很清楚,各位熟悉这方面的朋友能否指点一下,如何实现监听.

解决方案 »

  1.   

    定义一个实现监听器接口的类 实现里面的方法
    然后再用对象的addXXXListener方法 注册监听器应该是这样的吧。。 我新手
      

  2.   

    我也是新手,这样应该可以
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class A extends JFrame{
        JPanel pane;
        /** Creates a new instance of A */
        public A() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);       
            JbInit();
        }
        public void JbInit(){
            pane=(JPanel)getContentPane();
            JMenuBar menuBar=new JMenuBar();//菜单栏
            JMenu m1=new JMenu("帮助"); 
            JMenuItem item=new JMenuItem("关于");
            item.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){ 
                    if (e.getActionCommand().equals("关于"))
                        new About();                  
                  }
            });
            m1.add(item);
            menuBar.add(m1);
            pane.add(menuBar,BorderLayout.NORTH);
        }
        public static void main(String[]args){
            A a=new A();
            a.setSize(200,200);
            a.setTitle("MyTree");
            a.setVisible(true); 
            a.setResizable(false);
        }
    }
    class About{
         JDialog dialog;
         JOptionPane option;
         public About(){
             dialog=new JDialog(new CreateFrame(),"about",false);
             option=new JOptionPane("ghhhhhhhhhjgjhghjghjghj\n"
                                     + "nbvhtygkughjgjhg\n"
                                      +"opiu987hjky8087?",
                                    JOptionPane.QUESTION_MESSAGE,
                                    JOptionPane.YES_NO_OPTION);
             dialog.getContentPane().add(option);
             dialog.setSize(200,180);
             dialog.pack();
             dialog.show();
         }
     }
      

  3.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Test extends JFrame{
        JPanel pane;
        /** Creates a new instance of A */
        public Test() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);       
            JbInit();
        }
        public void JbInit(){
            pane=(JPanel)getContentPane();
            JMenuBar menuBar=new JMenuBar();//菜单栏
            JMenu m1=new JMenu("帮助"); 
            JMenuItem item=new JMenuItem("关于");
            item.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){ 
                    if (e.getActionCommand().equals("关于"))
                        new About();                  
                  }
            });
            m1.add(item);
            menuBar.add(m1);
            pane.add(menuBar,BorderLayout.NORTH);
        }
        public static void main(String[]args){
           Test a=new Test();
            a.setSize(200,200);
            a.setTitle("MyTree");
            a.setVisible(true); 
            a.setResizable(false);
        }
    }
    class About extends JFrame{
         
         public About(){
           setBounds(200,180,200,200);
             //可以根据需要添加组件
           
           setVisible(true);
         }
     }
      

  4.   

    addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    .............
    }
    为什么监听都要加上这么一段代码,而且如果是mouse或键盘所对应的代码又不一样了,怎样才能很好的区分开如果是mouse或键盘所对应的监听代码是什么?
      

  5.   

    addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    .............
    }
    为什么监听都要加上这么一段代码,而且如果是mouse或键盘所对应的代码又不一样了,怎样才能很好的区分开如果是mouse或键盘所对应的监听代码是什么?========================================addActionListener(ActionListener) - 类 java.awt.Button 中的方法 
    添加指定的操作侦听器,以接收来自此按钮的操作事件。 
    addActionListener(ActionListener) - 类 java.awt.List 中的方法 
    添加指定的操作侦听器以从此列表接收操作事件。 
    addActionListener(ActionListener) - 类 java.awt.MenuItem 中的方法 
    添加指定的操作侦听器,以从此菜单项接收操作事件。 
    addActionListener(ActionListener) - 类 java.awt.TextField 中的方法 
    添加指定的操作侦听器,以从此文本字段接收操作事件。 
    addActionListener(ActionListener) - 类 javax.swing.AbstractButton 中的方法 
    将一个 ActionListener 添加到按钮中。 
    addActionListener(ActionListener) - 接口 javax.swing.ButtonModel 中的方法 
    向按钮添加一个 ActionListener。 
    addActionListener(ActionListener) - 接口 javax.swing.ComboBoxEditor 中的方法 
    添加一个 ActionListener。 
    addActionListener(ActionListener) - 类 javax.swing.DefaultButtonModel 中的方法 
    将一个 ActionListener 添加到按钮中。 
    addActionListener(ActionListener) - 类 javax.swing.JComboBox 中的方法 
    添加 ActionListener。 
    addActionListener(ActionListener) - 类 javax.swing.JFileChooser 中的方法 
    向文件选择器添加一个 ActionListener。 
    addActionListener(ActionListener) - 类 javax.swing.JTextField 中的方法 
    添加指定的操作侦听器以从此文本字段接收操作事件。 
    addActionListener(ActionListener) - 类 javax.swing.plaf.basic.BasicComboBoxEditor 中的方法 addActionListener(ActionListener) - 类 javax.swing.Timer 中的方法 
    将一个操作侦听器添加到 Timer。 每种事件都有对应的监听器,不清楚时可以查API文档。