Java从入门到精通p257第二题下拉列表框中相应添加数组中的内容

public class Test2 extends JFrame{
String[] test = new String[4];
String[] test2 ={"煞笔","白痴","智障","蠢货"};
static JButton button = new JButton("添加");
MyComboBox combo = new MyComboBox();
JComboBox jc = new JComboBox(combo);
public Test2(){
	getContentPane().add(button,BorderLayout.SOUTH);
	
	button.addActionListener(combo);
	
	getContentPane().add(jc,BorderLayout.NORTH);		
}
public static void main(String[] args) {
	Test2 test1 = new Test2();
	test1.setSize(400,300);
	test1.setVisible(true);
}
class MyComboBox extends AbstractListModel implements ComboBoxModel,ActionListener{
	String selecteditem = null;
	int size=0;
	public void actionPerformed(ActionEvent e) {
		if(size<test.length){
			test[size]=test2[size];
			size++;
		}	
	}
	public Object getElementAt(int index){	
		System.out.println("index="+index);
		return test[index];
	}
	public int getSize(){
		return test.length;
	}
	public void setSelectedItem(Object item){
		System.out.println("item="+item);
		selecteditem = (String)item;
	}
	public Object getSelectedItem(){
		return selecteditem;
	}
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值