Java 内部类和匿名类 实现JButton动作 ActionListener类

 1 import javax.swing.*;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 
 5 public class ControlCircle2 extends JFrame {
 6     private JButton jbtEnlarge=new JButton("Enlarge");
 7     private JButton jbtShrink=new JButton("Shrink");
 8     private CirclePanel canvas=new CirclePanel();
 9     
10     public ControlCircle2(){
11         JPanel panel=new JPanel();
12         panel.add(jbtEnlarge);
13         panel.add(jbtShrink);
14         this.add(canvas,BorderLayout.CENTER);
15         this.add(panel, BorderLayout.SOUTH);
16         
17         jbtEnlarge.addActionListener(new EnlargeListener());
18         jbtShrink.addActionListener(new ShrinkListener());
19     }
20 
21     public static void main(String[] args) {
22         // TODO Auto-generated method stub
23         ControlCircle2 frame=new ControlCircle2();
24         frame.setTitle("ControlCircle2");
25         frame.setSize(200,200);
26         frame.setLocationRelativeTo(null);
27         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28         frame.setVisible(true);
29 
30     }
31     class ShrinkListener implements ActionListener{
32         @Override
33         public void actionPerformed(ActionEvent e) {
34             // TODO Auto-generated method stub
35             canvas.shrink();
36         }
37     }
38     class EnlargeListener implements ActionListener{
39         @Override
40         public void actionPerformed(ActionEvent e) {
41             // TODO Auto-generated method stub
42             canvas.enlarge();
43         }
44     }
45     
46 }
47 class CirclePanel extends JPanel {
48     private int radius=5;
49     public void enlarge(){
50         radius++;
51         repaint();
52     }
53     public void shrink(){
54         if(radius>=1){
55         radius--;
56         repaint();}
57         else
58             JOptionPane.showMessageDialog(null, "Radius can not less than 1.");
59     }
60     protected void paintComponent(Graphics g){
61         super.paintComponent(g);
62         g.drawOval(getWidth()/2-radius, getHeight()/2,
63                 2*radius, 2*radius);
64     }
65 
66 }

为了让actionPerformed方法可以访问到canvas变量,将EnlargeListener定义为ControlCircle2类的内部类。

匿名内部类是没有名字的内部类,它一步完成定义内部类和创建一个该类的实例。

 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 
 6 public class AnonymousListenerDemo extends JFrame {
 7     public AnonymousListenerDemo(){
 8         JButton jbtNew=new JButton("New");
 9         JButton jbtOpen=new JButton("Open");
10         JButton jbtSave=new JButton("Save");
11         JButton jbtPrint=new JButton("Print");
12         
13         JPanel panel=new JPanel();
14         panel.add(jbtNew);
15         panel.add(jbtOpen);
16         panel.add(jbtSave);
17         panel.add(jbtPrint);
18         
19         add(panel);
20         
21         jbtNew.addActionListener(new ActionListener() {
22             public void actionPerformed(ActionEvent e){
23                 System.out.println("Process New");
24             }
25         });//匿名内部类
26         
27         jbtOpen.addActionListener(new ActionListener(){
28             public void actionPerformed(ActionEvent e){
29                 System.out.println("Process Open");
30             }
31         });
32         
33         jbtSave.addActionListener(new ActionListener(){
34             public void actionPerformed(ActionEvent e){
35                 System.out.println("Process Save");
36             }
37         });
38         
39         jbtPrint.addActionListener(new ActionListener(){
40             public void actionPerformed(ActionEvent e){
41                 System.out.println("Process Print");
42             }
43         });
44     }
45 
46     public static void main(String[] args) {
47         // TODO Auto-generated method stub
48         AnonymousListenerDemo frame=new AnonymousListenerDemo();
49         frame.setTitle("AnonymousListenerDemo");
50         frame.pack();//程序没有使用setSize方法来设置框架的大小,而是使用pack方法,可以根据组件的大小来自动调整框架的大小。
51         frame.setLocationRelativeTo(null);
52         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
53         frame.setVisible(true);
54 
55     }
56 
57 }

 还可以只使用一个actionListener,通过获取ActionEvent的类别,来触发相应的动作。

 1 class ButtonListener implements ActionListener {
 2         @Override
 3         public void actionPerformed(ActionEvent e) {
 4             // TODO Auto-generated method stub
 5             if(e.getSource()==jbtNew)//使用getSource方法,获得事件的对象,然后使用对应的动作。
 6                 {System.out.println("Process New");
 7                 JOptionPane.showMessageDialog(null, "Process New");}
 8             else if(e.getSource()==jbtOpen)
 9                 {System.out.println("Process Open");
10                 JOptionPane.showMessageDialog(null, "Process Open");}
11             else if(e.getSource()==jbtSave)
12                 {System.out.println("Process Save");
13                 JOptionPane.showMessageDialog(null, "Process Save");}
14             else if(e.getSource()==jbtPrint)
15                 {System.out.println("Process Print");
16                 JOptionPane.showMessageDialog(null, "Process Print");}
17         }
18     }
原文地址:https://www.cnblogs.com/xingzhui/p/5724031.html