`
reymont
  • 浏览: 526331 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

addActionListener(this)

 
阅读更多

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方法里实现区分它们的方法。

分享到:
评论

相关推荐

    JAVA计算机小程序

    exit addActionListener this ; ok addActionListener this ; this setSize 540 70 ; this setTitle "简易计算机" ; this setVisible true ; this setResizable false ; } public static ...

    java实现简单的计算器

    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 ...

    Java课程设计----简易计算器.doc

    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 JButton(" 5...

    基本算法模拟程序java

    button_exp1.addActionListener(this); button_exp2=new JButton("归并分类"); button_exp2.setFont(new Font("黑体",Font.BOLD,30)); button_exp2.addActionListener(this); button_exp3=new JButton("循环赛...

    java_NoteBook

    /** * 添加文件的子菜单项添加到文件菜单中 */ menuFile = new Menu("文件"); mNew = new MenuItem(... mPageStyle.addActionListener(this); mPrint.addActionListener(this); mNew.addActionListener(this);

    java客户管理系统

    java的客户管理系统, ... jm1.addActionListener(this); jm2.addActionListener(this); jm3.addActionListener(this); jm4.addActionListener(this); jm5.addActionListener(this); jm6.addActionListener(this);

    基于java的JPEG图像生成器(课程设计)

    绘制图形.addActionListener(this); 保存.addActionListener(this); 画笔.addActionListener(this); 橡皮.addActionListener(this); 清除.addActionListener(this); 获取屏幕.addActionListener(this); 画笔...

    JAVA课程设计淘宝管理系统.doc

    //分别添加按钮和事件 bqingkong.addActionListener(this); p.add(bqingkong); btuige=new Button("back"); btuige.addActionListener(this); p.add(btuige); bkaigen=new Button(" "); bkaigen.addActionListener...

    java 写的浏览器 源代码

    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...

    ATM,java编写的

    File file; Hashtable 基本信息表; JPanel panel; ... JTextField zhanghaotf;... 确定.addActionListener(this); 退出= new JButton("退出"); 退出.addActionListener(this); con=getContentPane();

    java网上投票系统

    btVote.addActionListener(this); bttj=new Button("统计"); bttj.addActionListener(this); btCan=new Button("竞选班长"); btCan.addActionListener(this); btCan.setEnabled(false); vm=new VoteMoniter...

    记事本,Java代码

    itemNew.addActionListener(this); itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); itemOpen.addActionListener(this); itemOpen.setAccelerator(KeyStroke....

    JAVA编写的计算器

    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....

    制作登录界面

    mItemFlowCenter.addActionListener(this); mItemFlowRight = new JMenuItem("流式右布局"); mItemFlowRight.addActionListener(this); mItemBorder = new JMenuItem("边界布局"); mItemBorder....

    用JAVA写的简单GUI

    import java.awt.*; import javax.swing.*;... textField1.addActionListener(this); textField2.addActionListener(this); textField3.addActionListener(this); setVisible(true); } .......

    java课程设计

    nButton.addActionListener(this); nButton.setBounds(120,40,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("4"); nButton.addActionListener(this); nButton....

    Java wav音频播放器程序附源代码.rar

     playButton.addActionListener(this);  loopButton.addActionListener(this);  stopButton.addActionListener(this);  stopButton.setEnabled(false); //设置停止播放按钮不可用  //把播放按钮、循环播放按钮...

    计算器_java

    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);

Global site tag (gtag.js) - Google Analytics