swing 事件的问题
//创建的是单击Button 打印出下拉列表选中项
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudyEvent extends JPanel{
private JComboBox jcb;
private JButton jb;
StudyEvent(){
this.jb=new JButton( "打印 ");
this.jcb=new JComboBox();
this.jcb.addItem( "北京 ");
this.jcb.addItem( "上海 ");
this.jcb.addItem( "大连 ");
this.jcb.addItem( "哈尔滨 ");
this.add(this.jb);
this.add(this.jcb);
getEvent();
}
public void getEvent(){
this.jb.addActionListener(new StudyListener(this));
}
public void getPrint(){
//我想打印出下拉列表选中的项的文字例如北京,应该 怎么写
System.out.println(this.jcb.getActionCommand());
}
}
class StudyListener implements ActionListener{
private StudyEvent se;
StudyListener(StudyEvent se){
this.se=se;
}
public void actionPerformed(ActionEvent arg0) {
se.getPrint();
}
}
class StudyEventDemo{
public static void main(String args[]){
JFrame frame=new JFrame( "打印下拉列表文字 ");
frame.setSize(300,300);
frame.add(new StudyEvent());
frame.setVisible(true);
}
}
------解决方案-------------------- public void getPrint(){
System.out.println(this.jcb.getSelectedItem());
}
把类StudyEventDemo写作public的
StudyEvent的public不要