JAVA中对同一问题分别使用内部类与匿名内部类实现


JAVA中对同一问题分别使用内部类与匿名内部类实现,基于轻量级组件Swing中JComboBox组件来举例说明,希望能够有助于深入理解内部类与匿名内部类的区别以及其使用。

  1. package com.han;  
  2. import javax.swing.*;  
  3.   
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6.   
  7. /** 
  8.  * 设计了一个Swing窗体,其中包括了JComboBox组件(下拉列表框), 
  9.  * 在下面的代码中运用了内部类的手段。 
  10.  * @author HAN 
  11.  * 
  12.  */  
  13. @SuppressWarnings("serial")  
  14. public class SwingJComboBox extends JFrame{  
  15.   
  16.     public SwingJComboBox(){  
  17.         setLayout(null);  
  18.         setBounds(130,30,300,200);  
  19.         Container c=getContentPane();  
  20.         final MyComboBox obj1=new MyComboBox();  
  21.         @SuppressWarnings({ "unchecked""rawtypes" })  
  22.         JComboBox jc=new JComboBox(obj1);  
  23.         jc.setBounds(0,0,290,20);  
  24. //      System.out.println(obj1.getElementAt(0));   
  25.         jc.addActionListener(new ActionListener(){  
  26.             public void actionPerformed(ActionEvent arg0){  
  27.                 System.out.println(obj1.getSelectedItem());  
  28.             }  
  29.         });  
  30.         JCheckBox jck1=new JCheckBox("男");  
  31.         JCheckBox jck2=new JCheckBox("女",true);  
  32.         jck1.setBounds(100,80,40,20);  
  33.         jck2.setBounds(140,80,40,20);  
  34.         JButton jb1=new JButton("确定");  
  35.         JButton jb2=new JButton("取消");  
  36.         jb1.setBounds(80,130,60,30);  
  37.         jb2.setBounds(140,130,60,30);  
  38.         c.add(jc);  
  39.         c.add(jck1);  
  40.         c.add(jck2);  
  41.         c.add(jb1);  
  42.         c.add(jb2);  
  43.         setVisible(true);  
  44.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //very important !!   
  45.     }  
  46.       
  47.     @SuppressWarnings("rawtypes")  
  48.     class MyComboBox extends AbstractListModel implements ComboBoxModel {  
  49.         String selecteditem="军人证";  
  50.         String[] test={"身份证","军人证","学生证"};  
  51.         public void setSelectedItem(Object item){  
  52.             selecteditem=(String)item;  
  53.         }  
  54.         public Object getSelectedItem(){  
  55.             return selecteditem;  
  56.         }  
  57.         @Override  
  58.         public int getSize() {  
  59.             // TODO Auto-generated method stub   
  60.             return test.length;  
  61.         }  
  62.         @Override  
  63.         public Object getElementAt(int index) {  
  64.             // TODO Auto-generated method stub   
  65.             return test[index];  
  66.         }  
  67.           
  68.     }  
  69.       
  70.     public static void main(String[] args){  
  71.         new SwingJComboBox();  
  72.           
  73.     }  
  74. }  

 
  1. package com.han;  
  2. import javax.swing.*;  
  3. import javax.swing.event.ListDataListener;  
  4.   
  5. import java.awt.*;  
  6. import java.awt.event.*;  
  7.   
  8. /** 
  9.  * 设计了一个Swing窗体,其中包括了JComboBox组件(下拉列表框), 
  10.  * 在下面的代码中运用了匿名内部类的手段。 
  11.  * @author HAN 
  12.  * 
  13.  */  
  14. @SuppressWarnings("serial")  
  15. public class SwingJComboBox extends JFrame{  
  16.     public static String selectedItem;  
  17.     public SwingJComboBox(){  
  18.         setLayout(null);  
  19.         setBounds(130,30,300,200);  
  20.         Container c=getContentPane();  
  21. //      final MyComboBox obj1=new MyComboBox();   
  22.         @SuppressWarnings({ "unchecked""rawtypes" })  
  23.         JComboBox jc=new JComboBox(new ComboBoxModel(){  
  24.             String selecteditem="军人证";  
  25.             String[] test={"身份证","军人证","学生证"};  
  26. //          public void getItem(){   
  27. //              selectedItem=selecteditem;   
  28. //          }   
  29.             @Override  
  30.             public int getSize() {  
  31.                 // TODO Auto-generated method stub   
  32.                 return test.length;  
  33.             }  
  34.   
  35.             @Override  
  36.             public Object getElementAt(int index) {  
  37.                 // TODO Auto-generated method stub   
  38.                 return test[index];  
  39.             }  
  40.   
  41.             @Override  
  42.             public void setSelectedItem(Object anItem) {  
  43.                 // TODO Auto-generated method stub   
  44.                 selecteditem=(String) anItem;  
  45.             }  
  46.   
  47.             @Override  
  48.             public Object getSelectedItem() {  
  49.                 // TODO Auto-generated method stub   
  50. //              getItem();   
  51.                 selectedItem=selecteditem;  
  52.                 return selecteditem;  
  53.             }  
  54.   
  55.             @Override  
  56.             public void addListDataListener(ListDataListener l) {  
  57.                 // TODO Auto-generated method stub   
  58.                   
  59.             }  
  60.   
  61.             @Override  
  62.             public void removeListDataListener(ListDataListener l) {  
  63.                 // TODO Auto-generated method stub   
  64.                   
  65.             }  
  66.               
  67.         });  
  68.         jc.setBounds(0,0,290,20);  
  69. //      System.out.println(obj1.getElementAt(0));   
  70.         jc.addActionListener(new ActionListener(){  
  71.             public void actionPerformed(ActionEvent arg0){  
  72.                 System.out.println(selectedItem);  
  73.             }  
  74.         });  
  75.         JCheckBox jck1=new JCheckBox("男");  
  76.         JCheckBox jck2=new JCheckBox("女",true);  
  77.         jck1.setBounds(100,80,40,20);  
  78.         jck2.setBounds(140,80,40,20);  
  79.         JButton jb1=new JButton("确定");  
  80.         JButton jb2=new JButton("取消");  
  81.         jb1.setBounds(80,130,60,30);  
  82.         jb2.setBounds(140,130,60,30);  
  83.         c.add(jc);  
  84.         c.add(jck1);  
  85.         c.add(jck2);  
  86.         c.add(jb1);  
  87.         c.add(jb2);  
  88.         setVisible(true);  
  89.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //very important !!   
  90.     }  
  91.       
  92.       
  93.     public static void main(String[] args){  
  94.         new SwingJComboBox();  
  95.           
  96.     }  
  97. }  

相关内容