java核心技术卷 之处理按钮点击事件

        为了加深对事件委托模铟的理解,下面以一个响应按钮点击事件的简单示例来说明所需要知道的所有细节。在这个示例中,想要在一个面板中放置三个按钮,添加三个监听器对象用来作为按钮的动作监听器。 

        在这个情况下,用户点击面板上的任何一个按钮,相关的监听器对象就会接收到一 个ActionEvent对象,它表示有个按钮被点击了在示例程序中,监听器对象将改变面板的背景颜色。

        在演示如何监听按钮点击事件之前,首先需要讲解一下如何创建按钮以及如何将它们添加到而板中。可以通过在按钮构造器中指定一个标签字符串、一个图标或两项都指定来创建一个按钮。下面是两个示例:

       JButton yellowButton=new JButton("Yellow");
       JButton blueButton=new JButton("Blue");

       将按钮添加到面板中需要凋用add方法:

       buttonPanel.add(yellowButton);
       buttonPanel.add(blueButton);

       接下来需要增加让面板监听这些按钮的代码。这需要一个实现了ActicnListener接口的类。如前所述,应该包含一个actionPerformed方法,其签名为: 

       public void actionPerformed(ActionEvent event)
       当按钮被点击时,希望将而板的背累颜色设置为指定的颜色。这个颜色存储在监听器类中:

 private class ColorAction implements ActionListener{

        private Color BackgrpundColor;

        public ColorAction(Color c){

            BackgrpundColor=c;

        }

        public void actionPerformed(ActionEvent event){

            buttonPanel.setBackground(BackgrpundColor);

        }

    }

      然后,为每种颜色构造一个对象,并将这些对象设置为按钮监听器。

        //create button actions
        ColorAction yellowAction=new ColorAction(Color.YELLOW);
        ColorAction blueAction=new ColorAction(Color.BLUE);
        ColorAction redAction=new ColorAction(Color.red);
        //associate actions with buttons
        yellowButton.addActionListener(yellowAction);
        blueButton.addActionListener(blueAction);
        redButton.addActionListener(redAction);

        例如,如果一个用户在标有‚Yellow的按钮上点击了一下,yellowAction对象的actionPerformed方法就会被调用。这个对象的backgroundColor实例域被设控为Color.YELLOW, 现在就将面板的背景色设置为黄色了。

        这里还有一个需要考虑的问题。ColorAction对象不能访问buttonpane变量。可以采用两种方式解决这个问题。一个是将面板存储在ColorAction对象中,并在ColorAction的构造器中设置它;另一个是将ColorAction作为ButtonFrame类的内部类,这样一来,它的方法就自动地拥有访问外部面板的权限了。

       完整的代码:

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.*;

/**

 * Created by IBM on 2017/9/11.

 */

public class ButtonFrame extends JFrame {

    private JPanel buttonPanel;

    private static final int DEFAULT_WIDTH=300;

    private static final int DEFAULT_HEIGHT=200;

    public static void main(String[]args){

        ButtonFrame buttonFrame=new ButtonFrame();

        buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonFrame.setVisible(true);

    }

    public ButtonFrame(){

        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);


        JButton yellowButton=new JButton("Yellow");

        JButton blueButton=new JButton("Blue");

        JButton redButton=new JButton("Red");


        buttonPanel=new JPanel();

        //add buttons to panel

        buttonPanel.add(yellowButton);

        buttonPanel.add(blueButton);

        buttonPanel.add(redButton);


        add(buttonPanel);


        //create button actions

        ColorAction yellowAction=new ColorAction(Color.YELLOW);

        ColorAction blueAction=new ColorAction(Color.BLUE);

        ColorAction redAction=new ColorAction(Color.red);


        //associate actions with buttons

        yellowButton.addActionListener(yellowAction);

        blueButton.addActionListener(blueAction);

        redButton.addActionListener(redAction);

    }

    private class ColorAction implements ActionListener{

        private Color BackgrpundColor;

        public ColorAction(Color c){

            BackgrpundColor=c;

        }

        public void actionPerformed(ActionEvent event){

            buttonPanel.setBackground(BackgrpundColor);

        }

    }

}

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值