使用java语言实现点击按钮改变页面背景颜色

效果展示

在这里插入图片描述

代码分析

定义一个background类

继承自AWT的Frame类。同时定义4个按钮用于改变颜色。

public class background extends Frame {
    Button button1 = new Button("Red");
    Button button2 = new Button("Yellow");
    Button button3 = new Button("Blue");
    Button button4 = new Button("RandomColor");
}

定义background类

添加按钮和窗体。

add(button1); — —将创建的按钮添加到Frame中。

this.setLayout(new FlowLayout());— —为Frame设置布局管理器为FlowLayout,这意味着组件将按照添加的顺序从左到右排列,到达窗口边界时自动换行。

this.setSize(400,300);— —设置Frame的大小为400像素宽,300像素高。

this.setVisible(true);— —使窗口可见。

this.addWindowListener(new WindowAdapter();— —添加窗口监听器WindowAdapter,并重写windowClosing方法,当用户点击窗口关闭按钮时退出程序。

button1.addActionListener(new ActionListener();— —给button1添加事件监听器ActionListener。当点击红色按钮时,会通过导致事件的ActionEvent来调用actionPerformed方法,在方法中设置框架的背景色为红色。

类似地,给button2添加监听器,点击后会将背景颜色设置为黄色。

给button3添加监听器,点击后将背景颜色设置为蓝色。

给button4添加事件监听器。当此按钮被点击时,将生成一个随机颜色(通过随机RGB值)并设置为框架的背景颜色。

public background(){
        add(button1);
        add(button2);
        add(button3);
        add(button4);
        this.setLayout(new FlowLayout());
        this.setSize(400,300);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
            }
        });
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                background.this.setBackground(Color.red);
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                background.this.setBackground(Color.yellow);
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                background.this.setBackground(Color.blue);
            }
        });
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int R = (int)(Math.random()*256);
                int G = (int)(Math.random()*256);
                int B = (int)(Math.random()*256);
                Color color = new Color(R,G,B);
                background.this.setBackground(color);
            }
        });
    }

定义程序入口

new background();— —创建background类的实例,这将展示一个窗口,窗口中包含四个按钮;点击不同的按钮可以更改窗口的背景色。

public static void main(String[] args){
        new background();
    }

完整代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class background extends Frame {
    Button button1 = new Button("Red");
    Button button2 = new Button("Yellow");
    Button button3 = new Button("Blue");
    Button button4 = new Button("RandomColor");

    public background(){
        add(button1);
        add(button2);
        add(button3);
        add(button4);
        this.setLayout(new FlowLayout());
        this.setSize(400,300);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
            }
        });
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                background.this.setBackground(Color.red);
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                background.this.setBackground(Color.yellow);
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                background.this.setBackground(Color.blue);
            }
        });
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int R = (int)(Math.random()*256);
                int G = (int)(Math.random()*256);
                int B = (int)(Math.random()*256);
                Color color = new Color(R,G,B);
                background.this.setBackground(color);
            }
        });
    }
    public static void main(String[] args){
        new background();
    }

}

  • 23
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值