达内LOGO和北京达内网址达内科技培训项目:Java培训 3G培训 Android培训 软件测试培训北京达内服务电话
java培训
JAVA中使用内部类与匿名内部类实现

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

view plain
package com.han;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

/**
 * 设计了一个Swing窗体,其中包括了JComboBox组件(下拉列表框),
 * 在下面的代码中运用了内部类的手段。
 * @author HAN
 *
 */
@SuppressWarnings("serial")
public class SwingJComboBox extends JFrame{

    public SwingJComboBox(){
        setLayout(null);
        setBounds(130,30,300,200);
        Container c=getContentPane();
        final MyComboBox obj1=new MyComboBox();
        @SuppressWarnings({ "unchecked", "rawtypes" })
        JComboBox jc=new JComboBox(obj1);
        jc.setBounds(0,0,290,20);
//      System.out.println(obj1.getElementAt(0));
        jc.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                System.out.println(obj1.getSelectedItem());
            }
        });
        JCheckBox jck1=new JCheckBox("男");
        JCheckBox jck2=new JCheckBox("女",true);
        jck1.setBounds(100,80,40,20);
        jck2.setBounds(140,80,40,20);
        JButton jb1=new JButton("确定");
        JButton jb2=new JButton("取消");
        jb1.setBounds(80,130,60,30);
        jb2.setBounds(140,130,60,30);
        c.add(jc);
        c.add(jck1);
        c.add(jck2);
        c.add(jb1);
        c.add(jb2);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //very important !!
    }

    @SuppressWarnings("rawtypes")
    class MyComboBox extends AbstractListModel implements ComboBoxModel {
        String selecteditem="军人证";
        String[] test={"身份证","军人证","学生证"};
        public void setSelectedItem(Object item){
            selecteditem=(String)item;
        }
        public Object getSelectedItem(){
            return selecteditem;
        }
        @Override
        public int getSize() {
            // TODO Auto-generated method stub
            return test.length;
        }
        @Override
        public Object getElementAt(int index) {
            // TODO Auto-generated method stub
            return test[index];
        }

    }

    public static void main(String[] args){
        new SwingJComboBox();

    }
}

view plain
package com.han;
import javax.swing.*;
import javax.swing.event.ListDataListener;

import java.awt.*;
import java.awt.event.*;

/**
 * 设计了一个Swing窗体,其中包括了JComboBox组件(下拉列表框),
 * 在下面的代码中运用了匿名内部类的手段。
 * @author HAN
 *
 */
@SuppressWarnings("serial")
public class SwingJComboBox extends JFrame{
    public static String selectedItem;
    public SwingJComboBox(){
        setLayout(null);
        setBounds(130,30,300,200);
        Container c=getContentPane();
//      final MyComboBox obj1=new MyComboBox();
        @SuppressWarnings({ "unchecked", "rawtypes" })
        JComboBox jc=new JComboBox(new ComboBoxModel(){
            String selecteditem="军人证";
            String[] test={"身份证","军人证","学生证"};
//          public void getItem(){
//              selectedItem=selecteditem;
//          }
            @Override
            public int getSize() {
                // TODO Auto-generated method stub
                return test.length;
            }

            @Override
            public Object getElementAt(int index) {
                // TODO Auto-generated method stub
                return test[index];
            }

            @Override
            public void setSelectedItem(Object anItem) {
                // TODO Auto-generated method stub
                selecteditem=(String) anItem;
            }

            @Override
            public Object getSelectedItem() {
                // TODO Auto-generated method stub
//              getItem();
                selectedItem=selecteditem;
                return selecteditem;
            }

            @Override
            public void addListDataListener(ListDataListener l) {
                // TODO Auto-generated method stub

            }

            @Override
            public void removeListDataListener(ListDataListener l) {
                // TODO Auto-generated method stub

            }

        });
        jc.setBounds(0,0,290,20);
//      System.out.println(obj1.getElementAt(0));
        jc.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                System.out.println(selectedItem);
            }
        });
        JCheckBox jck1=new JCheckBox("男");
        JCheckBox jck2=new JCheckBox("女",true);
        jck1.setBounds(100,80,40,20);
        jck2.setBounds(140,80,40,20);
        JButton jb1=new JButton("确定");
        JButton jb2=new JButton("取消");
        jb1.setBounds(80,130,60,30);
        jb2.setBounds(140,130,60,30);
        c.add(jc);
        c.add(jck1);
        c.add(jck2);
        c.add(jb1);
        c.add(jb2);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //very important !!
    }


    public static void main(String[] args){
        new SwingJComboBox();

    }
}