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();
}
}