歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> JAVA中對同一問題分別使用內部類與匿名內部類實現

JAVA中對同一問題分別使用內部類與匿名內部類實現

日期:2017/3/1 10:44:26   编辑:Linux編程

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

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

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