首页 > 其他 > 详细

事件处理

时间:2019-07-05 12:25:17      阅读:91      评论:0      收藏:0      [点我收藏+]

任务:完成一个按钮的事件处理程序,实现功能自拟,例如:改变窗口的背景颜色,改变按钮的位置等等。

 

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Assembly{ 
}
public class BackgroundColor implements ActionListener{
    JFrame f;
    JPanel p;
    JPanel p1,p2,p3,p4,p5;
    BackgroundColor() {
        JButton b=new JButton("改变背景色");
        f=new JFrame();
        p=new JPanel(new GridLayout(3,3,10,10));
        p1=new JPanel();
        p2=new JPanel();
        p3=new JPanel();
        p4=new JPanel();
        p5=new JPanel(new GridLayout());
        f.add(p);
        p.add(p1);
        p.add(p2);
        p.add(p3);
        p.add(p4);
        p.add(p5);
        p5.add(b);
        p.setBackground(new Color(255,255,255));
        p1.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p2.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p3.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p4.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p5.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        f.setVisible(true);
        f.setSize(500, 500);
        b.addActionListener(this);
    }

    public static void main(String[] args) {
        new RandomColors();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        p1.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p2.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p3.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p4.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
        p5.setBackground(new Color((int)(0+Math.random()*255),(int)(0+Math.random()*255),(int)(0+Math.random()*255)));
    }
}

 

事件处理

原文:https://www.cnblogs.com/ShijouTakane/p/11136913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!