addActionListener(this)
看书的时候看到这行代码,觉得很奇怪
不是一般写成
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
name.setText(selectedName);
}
});
要一个ActionListener对象,结果一看
public class YourClass implements ActionListener {
... // Other code for your class
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("I was selected.");
}
// Code within some method
JButton button = new JButton(...);
button.addActionListener(this);
// More code within some method
}
自己的类都实现了ActionListener,当然就可以addActionListener(this)
不过在YourClass里面如果有很多Button的话,你就得在actionPerformed方法里实现区分它们的方法。
分享到:
相关推荐
exit addActionListener this ; ok addActionListener this ; this setSize 540 70 ; this setTitle "简易计算机" ; this setVisible true ; this setResizable false ; } public static ...
s1.addActionListener(this); s2=new JButton(" 2 "); s2.addActionListener(this); s3=new JButton(" 3 "); s3.addActionListener(this); s4=new JButton(" 4 "); s4.addActionListener(this); s5=new ...
button_exp1.addActionListener(this); button_exp2=new JButton("归并分类"); button_exp2.setFont(new Font("黑体",Font.BOLD,30)); button_exp2.addActionListener(this); button_exp3=new JButton("循环赛...
/** * 添加文件的子菜单项添加到文件菜单中 */ menuFile = new Menu("文件"); mNew = new MenuItem(... mPageStyle.addActionListener(this); mPrint.addActionListener(this); mNew.addActionListener(this);
java的客户管理系统, ... jm1.addActionListener(this); jm2.addActionListener(this); jm3.addActionListener(this); jm4.addActionListener(this); jm5.addActionListener(this); jm6.addActionListener(this);
button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Button clicked! ID: " + this.id); // "this" now refers to the anonymous ...
绘制图形.addActionListener(this); 保存.addActionListener(this); 画笔.addActionListener(this); 橡皮.addActionListener(this); 清除.addActionListener(this); 获取屏幕.addActionListener(this); 画笔...
fullscreenItem.addActionListener(this); sourceItem.addActionListener(this); picView.addActionListener(this); reloadItem.addActionListener(this); button.addActionListener(this); jurl....
findenter.addActionListener(this); //声明对话框中上确定按钮,并注册事件 replb=new JButton("确定"); replb.addActionListener(this); mainpane=(JPanel)this.getContentPane(); mainpane.setLayout...
File file; Hashtable 基本信息表; JPanel panel; ... JTextField zhanghaotf;... 确定.addActionListener(this); 退出= new JButton("退出"); 退出.addActionListener(this); con=getContentPane();
itemNew.addActionListener(this); itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); itemOpen.addActionListener(this); itemOpen.setAccelerator(KeyStroke....
bc.addActionListener(this); bd.addActionListener(this); b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); ...
btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("2"); panel.add(btn); btn.addActionListener(this); btn.addKeyListener(this); btn=new Button("3"); panel.add(btn); btn....
import java.awt.*; import javax.swing.*;... textField1.addActionListener(this); textField2.addActionListener(this); textField3.addActionListener(this); setVisible(true); } .......
nButton.addActionListener(this); nButton.setBounds(120,40,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("4"); nButton.addActionListener(this); nButton....
playButton.addActionListener(this); loopButton.addActionListener(this); stopButton.addActionListener(this); stopButton.setEnabled(false); //设置停止播放按钮不可用 //把播放按钮、循环播放按钮...
butcf.addActionListener(this); Butt.add(butcf); Button butc = new Button("*"); butc.setBounds(165,15,40,28); butc.setFont(new java.awt.Font("Dialog", 1, 25)); butc.addActionListener(this);...
运用java编写的小程序。部分代码:f=new Frame("电话薄"); f.setSize(640,480); Panel p=new Panel(); p.add(new Label("姓名"));... b1.addActionListener(this); b2.addActionListener(this);
SetHouseNum.addActionListener(this); SetEmpty.addActionListener(this); Delete.addActionListener(this); Bujiaoyajin.addActionListener(this); // 加入buttongroup,实现单选 bg.add(SetClean); ...