J2SE视频笔记(12)

J2SE视频笔记(12)

第三十六讲

Swing事件处理

  • ActionListener事件:用于接收操作事件的侦听器接口。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    class JButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
    // getActionCommand返回与此动作相关的命令字符串
    System.out.println(e.getActionCommand());
    JOptionPane.showMessageDialog(null, "我被点击了");
    }
    }
    public class EventTest1 {
    public static void main(String[] args) {
    JFrame jfm=new JFrame("Swing事件测试");
    JButton jb=new JButton("我是一个按钮");
    // Also,这个也有两种实现方式
    JButtonListener jButtonListener=new JButtonListener();
    jb.addActionListener(jButtonListener); //添加事件监听
    // 第二种,匿名内部类
    jb.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    }
    });
    jfm.add(jb);
    jfm.setLocation(400,200);
    jfm.setSize(500,200);
    jfm.setVisible(true);
    jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
  • WindowListener事件:是用于监听窗口事件的监听器接口。
  • 我们可以使用继承WindowListener接口的方法或者是使用匿名类的方法来实现对窗口的监听

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import javax.swing.JFrame;
    /**
    * WindowListener
    * @author hclmaster
    *
    */
    class MyWindowListener implements WindowListener{
    @Override
    public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println("窗口被选中...");
    }
    @Override
    public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println("窗口被关闭");
    }
    @Override
    public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println("窗口正在关闭...");
    }
    @Override
    public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println("窗口选中被取消...");
    }
    @Override
    public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println("窗口最小化恢复...");
    }
    @Override
    public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println("窗口最小化...");
    }
    @Override
    public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub
    System.out.println("窗口被打开");
    }
    }
    public class EventTest2 {
    public static void main(String[] args) {
    JFrame jfm=new JFrame("Swing事件测试");
    // 以下是实现的第一种方法
    MyWindowListener myWindowListener=new MyWindowListener();
    jfm.addWindowListener(myWindowListener);
    // 或者我们可以不用自己再创建一个类而是使用匿名内部类,如下所示
    jfm.addWindowListener(new WindowListener() {
    @Override
    public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub
    }
    });
    jfm.setLocation(400,200);
    jfm.setSize(500,200);
    jfm.setVisible(true);
    jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
  • 适配器类:

  • 如果要使用WindowListener的话,那么我们就得要写上面那样的那么多的一大堆的方法,所以我们可以使用适配器类,使用它的话我们可以选择只实现其中的某几个
  • 它也和上面那个一样,有两种实现方法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    // 继承适配器类
    class MyWindowAdapter extends WindowAdapter{
    @Override
    public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    super.windowClosing(e);
    System.out.println("窗口关闭....");
    }
    }
    public class EventTest3 {
    public static void main(String[] args) {
    JFrame jfm=new JFrame("Swing事件测试");
    // 这个也有2种实现方式,这个是第一种
    MyWindowAdapter myWindowAdapter=new MyWindowAdapter();
    jfm.addWindowListener(myWindowAdapter);
    // 这个是第二种,使用匿名内部类
    jfm.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    super.windowClosing(e);
    System.out.println("窗口关闭...");
    }
    });
    jfm.setLocation(400,200);
    jfm.setSize(500,200);
    jfm.setVisible(true);
    jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }

最后附上快捷键: 如果要快速的导入一大堆方法的话,使用alt+shift+s,然后选择’overide and implements methods’
多行备注快捷方式:ctrl+shift+/

热评文章