JAVA中對同一問題分別使用內部類與匿名內部類實現,基於輕量級組件Swing中JComboBox組件來舉例說明,希望能夠有助於深入理解內部類與匿名內部類的區別以及其使用。
- 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();
-
- }
- }
- 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();
-
- }
- }