熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JAVA中使用內部類與匿名內部類實現

2022-06-13   來源: Java核心技術 

  JAVA中對同一問題分別使用內部類與匿名內部類實現基於輕量級組件Swing中JComboBox組件來舉例說明希望能夠有助於深入理解內部類與匿名內部類的區別以及其使用

  view plain
package comhan;
import javaxswing*;

  import javaawt*;
import javaawtevent*;

  /**
 * 設計了一個Swing窗體其中包括了JComboBox組件(下拉列表框)
 * 在下面的代碼中運用了內部類的手段
 * @author HAN
 *
 */
@SuppressWarnings(serial)
public class SwingJComboBox extends JFrame{

  public SwingJComboBox(){
        setLayout(null);
        setBounds();
        Container c=getContentPane();
        final MyComboBox obj=new MyComboBox();
        @SuppressWarnings({ unchecked rawtypes })
        JComboBox jc=new JComboBox(obj);
        jcsetBounds();
//      Systemoutprintln(objgetElementAt());
        jcaddActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg){
                Systemoutprintln(objgetSelectedItem());
            }
        });
        JCheckBox jck=new JCheckBox();
        JCheckBox jck=new JCheckBox(true);
        jcksetBounds();
        jcksetBounds();
        JButton jb=new JButton(確定);
        JButton jb=new JButton(取消);
        jbsetBounds();
        jbsetBounds();
        cadd(jc);
        cadd(jck);
        cadd(jck);
        cadd(jb);
        cadd(jb);
        setVisible(true);
        setDefaultCloseOperation(WindowConstantsEXIT_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 Autogenerated method stub
            return testlength;
        }
        @Override
        public Object getElementAt(int index) {
            // TODO Autogenerated method stub
            return test[index];
        }

  }

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

  }
}

  view plain
package comhan;
import javaxswing*;
import javaxswingeventListDataListener;

  import javaawt*;
import javaawtevent*;

  /**
 * 設計了一個Swing窗體其中包括了JComboBox組件(下拉列表框)
 * 在下面的代碼中運用了匿名內部類的手段
 * @author HAN
 *
 */
@SuppressWarnings(serial)
public class SwingJComboBox extends JFrame{
    public static String selectedItem;
    public SwingJComboBox(){
        setLayout(null);
        setBounds();
        Container c=getContentPane();
//      final MyComboBox obj=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 Autogenerated method stub
                return testlength;
            }

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

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

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

  @Override
            public void addListDataListener(ListDataListener l) {
                // TODO Autogenerated method stub

  }

  @Override
            public void removeListDataListener(ListDataListener l) {
                // TODO Autogenerated method stub

  }

  });
        jcsetBounds();
//      Systemoutprintln(objgetElementAt());
        jcaddActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg){
                Systemoutprintln(selectedItem);
            }
        });
        JCheckBox jck=new JCheckBox();
        JCheckBox jck=new JCheckBox(true);
        jcksetBounds();
        jcksetBounds();
        JButton jb=new JButton(確定);
        JButton jb=new JButton(取消);
        jbsetBounds();
        jbsetBounds();
        cadd(jc);
        cadd(jck);
        cadd(jck);
        cadd(jb);
        cadd(jb);
        setVisible(true);
        setDefaultCloseOperation(WindowConstantsEXIT_ON_CLOSE); //very important !!
    }

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

  }
}


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26647.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.