JAVA中對同一問題分別使用內部類與匿名內部類實現
view plain
package com
import javax
import java
import java
/**
* 設計了一個Swing窗體
* 在下面的代碼中運用了內部類的手段
* @author HAN
*
*/
@SuppressWarnings(
public class SwingJComboBox extends JFrame{
public SwingJComboBox(){
setLayout(null);
setBounds(
Container c=getContentPane();
final MyComboBox obj
@SuppressWarnings({
JComboBox jc=new JComboBox(obj
jc
// System
jc
public void actionPerformed(ActionEvent arg
System
}
});
JCheckBox jck
JCheckBox jck
jck
jck
JButton jb
JButton jb
jb
jb
c
c
c
c
c
setVisible(true);
setDefaultCloseOperation(WindowConstants
}
@SuppressWarnings(
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
return test
}
@Override
public Object getElementAt(int index) {
// TODO Auto
return test[index];
}
}
public static void main(String[] args){
new SwingJComboBox();
}
}
view plain
package com
import javax
import javax
import java
import java
/**
* 設計了一個Swing窗體
* 在下面的代碼中運用了匿名內部類的手段
* @author HAN
*
*/
@SuppressWarnings(
public class SwingJComboBox extends JFrame{
public static String selectedItem;
public SwingJComboBox(){
setLayout(null);
setBounds(
Container c=getContentPane();
// final MyComboBox obj
@SuppressWarnings({
JComboBox jc=new JComboBox(new ComboBoxModel(){
String selecteditem=
String[] test={
// public void getItem(){
// selectedItem=selecteditem;
// }
@Override
public int getSize() {
// TODO Auto
return test
}
@Override
public Object getElementAt(int index) {
// TODO Auto
return test[index];
}
@Override
public void setSelectedItem(Object anItem) {
// TODO Auto
selecteditem=(String) anItem;
}
@Override
public Object getSelectedItem() {
// TODO Auto
// getItem();
selectedItem=selecteditem;
return selecteditem;
}
@Override
public void addListDataListener(ListDataListener l) {
// TODO Auto
}
@Override
public void removeListDataListener(ListDataListener l) {
// TODO Auto
}
});
jc
// System
jc
public void actionPerformed(ActionEvent arg
System
}
});
JCheckBox jck
JCheckBox jck
jck
jck
JButton jb
JButton jb
jb
jb
c
c
c
c
c
setVisible(true);
setDefaultCloseOperation(WindowConstants
}
public static void main(String[] args){
new SwingJComboBox();
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26647.html