处理按钮点击事件——事件处理

java核心技术卷——事件处理笔记

package button;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 按钮监听
 */
public class ButtonTest extends JFrame{
    private static final long serialVersionUID = 1L;
    
    //在核心卷中没有这段代码,导致程序不能运行,根据程序,可以自己写出这段代码
    public static void main(String[] args){
        ButtonTest buttonTest = new ButtonTest();
        buttonTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buttonTest.setVisible(true);
    }
    private JPanel buttonPanel;
    private static final int DEFAULT_WIDTH = 700;
    private static final int DEFAULT_HEIGHT = 500;

    public ButtonTest() {
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        //创建三个按钮
        JButton yellowButton = new JButton("Yelllow");
        JButton blueButton = new JButton("Blue");
        JButton redButton = new JButton("Red");

        buttonPanel = new JPanel();

        //将按钮添加到面板中
        buttonPanel.add(yellowButton);
        buttonPanel.add(blueButton);
        buttonPanel.add(redButton);

        add(buttonPanel);

        //设置按钮对应的事件
        ColorAction yellowAction = new ColorAction(Color.YELLOW);
        ColorAction blueAction = new ColorAction(Color.BLUE);
        ColorAction redAction = new ColorAction(Color.RED);

        //把上面的事件添加到按钮监听器上
        yellowButton.addActionListener(yellowAction);
        blueButton.addActionListener(blueAction);
        redButton.addActionListener(redAction);
    }

    /**
     * 设置按钮对应事件的对象
     */
    private class ColorAction implements ActionListener {
        private Color backgroundColor;

        public ColorAction(Color c) {       //初始化对象
            backgroundColor = c;
        }

        public void actionPerformed(ActionEvent event) {       //改变面板的颜色,事件处理方法
            buttonPanel.setBackground(backgroundColor);  
        }
    }
    
}

对上面程序进行改进以及简化

前面的程序中,每个按钮对象都经历了创建、添加到按钮对象中、实现按钮操作这几部分。所以可以单独设置一个方法,将这些重复的操作同一起来,已达到简化的目的。在新方法中使用了匿名类,实现了与ActionListener类的接口,类里面的方法是actionPerformed()。最后不要忘记add(buttonPanel); 将所有的按钮都显示在画板上。

package button;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 按钮监听事件并处理
 */
public class ButtonPro extends JFrame {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {        //程序入口
        ButtonPro buttonPro = new ButtonPro();
        buttonPro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buttonPro.setVisible(true);
    }

    private JPanel buttonPanel = new JPanel();      //添加按钮的对象
    private static final int DEFAULT_WIDTH = 700;
    private static final int DEFAULT_HEIGHT = 500;

    public ButtonPro() {        //初始化函数
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        makeButton("yellow", Color.YELLOW);
        makeButton("blue", Color.BLUE);
        makeButton("red", Color.RED);

        add(buttonPanel);       //将所有按钮添加到画板上
    }

    public void makeButton(String name, final Color backgroundColor){   //每个按钮的属性设置以及它要响应的操作
        JButton button = new JButton(name);
        buttonPanel.add(button);
        // 创建一个实现ActionListener的接口的匿名类,方法是actionPerformed
        button.addActionListener(new ActionListener() {         //按钮监听器
            public void actionPerformed(ActionEvent event){
                buttonPanel.setBackground(backgroundColor);
            }
        });

    }

}

在这里插入图片描述

发布了176 篇原创文章 · 获赞 46 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43207025/article/details/104175655