JComboBox box=new JComboBox();
box.addItem("one");
box.addItem("two");
box.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println(((JComboBox)e.getSource()).getSelectedItem());
}
}
);或者可以强行转换
JComboBox cmb=(JComboBox)(e.getSource());
String s=(String)cmb.getSelectedItem();
System.out.println(s);
box.addItem("one");
box.addItem("two");
box.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println(((JComboBox)e.getSource()).getSelectedItem());
}
}
);或者可以强行转换
JComboBox cmb=(JComboBox)(e.getSource());
String s=(String)cmb.getSelectedItem();
System.out.println(s);
实际上我感觉你得到了对象就应该可以得到这个对像的标题或text属性。
最后一种方法,你对每个对象setCommand,这个属性是String型,加入时设置它的这个属性,让它和显示名字一样。
值得注意的是,如果JComboBox里存放的是字符串或实现了toString()方法的对象,就没问题,如果是自定义对象,必须为其实现toString()方法。
------------------------首先你在这个Frame1中必须能得到另一个Frame2的对象句柄,如何得到,可以在Frame1的构造函数中传入也可以...方法很多,前提是必须在构建Frame2时,要保留一个指向它,而且可以为Frame1所用的句柄。
要得到控件值,你可以把控件设为public,也可以为其实现一个getxxxx方法返回其句柄。