以上是我写的一段简单的代码 其中监听器的作用 是让下拉菜单中显示 数组love中的元素 数组只能在初始化时操package lucky.first.lianxi;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class lianxi132 extends JFrame{
private JPanel panel1=new JPanel();
private JPanel panel2=new JPanel();
static String love[];
public static void shuzu(){
lianxi132 l=new lianxi132();
l.love={"身份证号","军人证号","护照号"};
}
public lianxi132(){
setTitle("监听");
setSize(200,150);
setVisible(true);
Container cp=getContentPane();
JComboBox jc=new JComboBox(new MycomboBox());
JButton jb=new JButton("显示吧 MY主!");
jb.setPreferredSize(new Dimension(150,20));
jb.addActionListener(new jbAction());
setLayout(new BorderLayout());
panel1.add(jc);
panel2.add(jb);
cp.add(BorderLayout.NORTH,panel1);
cp.add(BorderLayout.CENTER,panel2);
}
class jbAction implements ActionListener{
public void actionPerformed(ActionEvent arg0){
lianxi132 b=new lianxi132();
b.shuzu();
}
}
class MycomboBox extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
public Object getElementAt(int i){
return love[i];
}
public int getSize(){
return love.length;
}
public void setSelectedItem(Object item){
selecteditem=(String)item;
}
public Object getSelectedItem(){
return selecteditem;
}
}
public static void main(String[] args){
new lianxi132();
}
}
回首忆惘然
相关分类