获取事件监听需要获取实现ActionListener接口的方法,
public class SimpleEvent extends JFrame{
private JButton jb=new JButton("我是按钮,点击我");
public SimpleEvent(){
setLayout(null);
setVisible(true);
setSize(200, 100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container c=getContentPane();
c.add(jb);
jb.setBounds(10, 10, 300, 30);
jb.addActionListener(new jbAction());
}
class jbAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
jb.setText("我被点击了");
}
}
public static void main(String[] args) {
new SimpleEvent();
}
}
时间: 2024-11-08 11:44:04