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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM